R
rafaellasav
i have a dictionary with names and ages for each name. I want to write a function that takes in an age and returns the names of all the people who are that age.
please help
please help
i have a dictionary with names and ages for each name.
I want to write a function that takes in an age and returns
the names of all the people who are that age.
please help
Le 08.12.2013 18:59, (e-mail address removed) a �crit :
i have a dictionary with names and ages for each name.I want to write a function that takes in an age and returnsthe names of all the people who are that age.please help
ageDict = { 'john':42, 'jane':36, 'paul':42 }
peopleWithAge = lambda age: [ name for name in ageDict if
ageDict[name]==age]
['paul', 'john']Le 08.12.2013 18:59, (e-mail address removed) a �crit :
i have a dictionary with names and ages for each name.I want to write a function that takes in an age and returnsthe names of all the people who are that age.please help
ageDict = { 'john':42, 'jane':36, 'paul':42 }
peopleWithAge = lambda age: [ name for name in ageDict if
ageDict[name]==age]
sorry but i'm new to python ;p
1. it has to be in a form of a function called people and
2. how this code takes in an age and returns the names?ageDict = { 'john':42, 'jane':36, 'paul':42 }
people = lambda age: [ name for name in ageDict if ... ageDict[name]==age]
people(42)
i have a dictionary with names and ages for each name. I want to write a function that takes in an age and returns the names of all the people who are that age.
please help
i have a dictionary with names and ages for each name. I want to write a
function that takes in an age and returns the names of all the people who are
that age.
please help
Le 08.12.2013 18:59, (e-mail address removed) a �crit :
i have a dictionary with names and ages for each name.I want to write a function that takes in an age and returnsthe names of all the people who are that age.please help
ageDict = { 'john':42, 'jane':36, 'paul':42 }
peopleWithAge = lambda age: [ name for name in ageDict if
ageDict[name]==age]
sorry but i'm new to python ;p
1. it has to be in a form of a function called people and
2. how this code takes in an age and returns the names?
Welcome to the python list. Thanks for posting a question.i have a dictionary with names and ages for each name. I want to write a function that takes in an age and returns the names of all the people who are that age.
please help
Welcome to the python list. Thanks for posting a question.
If you were hoping for one of us to write the program for you ... well
that's not what we do on this list.
Please post the code you have so far and tell us exactly where you need
help.
Also tell us what version of Python, what OS, and what you use to write
and run Python programs.
On 12/8/2013 12:59 PM, (e-mail address removed) wrote:
Welcome to the python list. Thanks for posting a question.
If you were hoping for one of us to write the program for you ... well
that's not what we do on this list.
Please post the code you have so far and tell us exactly where you need
Also tell us what version of Python, what OS, and what you use to write
and run Python programs.
name = ['Alice', 'Bob', 'Cathy', 'Dan', 'Ed', 'Frank', 'Gary', 'Helen', 'Irene', 'Jack', 'Kelly', 'Larry']
age = [20, 21, 18, 18, 19, 20, 20, 19, 19, 19, 22, 19]
dic={}
def combine_lists(name,age):
for i in range(len(name)):
dic[name]= age
combine_lists(name,age)
print dic
def people(age):
people=lambda age: [name for name in dic if dic[name]==age]
people(20)
this is the code i have so far(with the help of the first post ;p). i understand how a function and a dictionary works and what I'm asked to find. but i don't get the lambda age part. and this code doesn't give me any result
Welcome to the python list. Thanks for posting a question.
If you were hoping for one of us to write the program for you ... well
that's not what we do on this list.
Please post the code you have so far and tell us exactly where you need
help.
Also tell us what version of Python, what OS, and what you use to write
and run Python programs.
name = ['Alice', 'Bob', 'Cathy', 'Dan', 'Ed', 'Frank', 'Gary', 'Helen', 'Irene', 'Jack', 'Kelly', 'Larry']
age = [20, 21, 18, 18, 19, 20, 20, 19, 19, 19, 22, 19]
dic={}
def combine_lists(name,age):
for i in range(len(name)):
dic[name]= age
combine_lists(name,age)
print dic
def people(age):
people=lambda age: [name for name in dic if dic[name]==age]
people(20)
this is the code i have so far(with the help of the first post ;p). i understand how a function and a dictionary works and what I'm asked to find. but i don't get the lambda age part. and this code doesn't give me any result
name = ['Alice', 'Bob', 'Cathy', 'Dan', 'Ed', 'Frank', 'Gary', 'Helen', 'Irene', 'Jack', 'Kelly', 'Larry']On 12/8/2013 12:59 PM, (e-mail address removed) wrote:
i have a dictionary with names and ages for each name. I want to write a function that takes in an age and returns the names of all the peoplewho are that age.
please help
Welcome to the python list. Thanks for posting a question.
If you were hoping for one of us to write the program for you ... well
that's not what we do on this list.
Please post the code you have so far and tell us exactly where you need
help.
Also tell us what version of Python, what OS, and what you use to write
and run Python programs.age = [20, 21, 18, 18, 19, 20, 20, 19, 19, 19, 22, 19]dic={}
def combine_lists(name,age):for i in range(len(name)):
people=lambda age: [name for name in dic if dic[name]==age]people(20)
this is the code i have so far(with the help of the first post ;p). i understand how a function and a dictionary works and what I'm asked to find.but i don't get the lambda age part. and this code doesn't give me any result
To return a value from a function, you need to use the "return"
statement with the value you want to pass back out. You're not doing
that here. Also, you're using a lot of shorthand stuff that you should
probably avoid until you're more comfortable with the language
* Lambda is shorthand for a function. foo = lambda bar : bar + 2 is
the same thing as the function
def foo(bar) :
return bar + 2
* a list comprehension is short-hand for a loop. spam = [foo for foo
in bar if baz(foo)] is the same thing as
spam = []
for foo in bar :
if baz(foo) :
spam.append(foo)
You don't need a lambda here- just call the code that you need to call directly.
but when i wrote these lines it returns me an error
Traceback (most recent call last):
File "/Users/rafaellasavva/Desktop/people.py", line 19, in <module>
print 'Dan' in people(18) and 'Cathy' in people(18)
TypeError: argument of type 'NoneType' is not utterable
do you know what it might be wrong?
i get it, thanks a lot i wrote a different one and it works
def people(age):
people=[name for name in dic if dic[name]==age]
print(people)
people(20)
i have one last question
it asks me to test my program function by running these lines:
print ’Dan’ in people(18) and ’Cathy’ in people(18)
print ’Ed’ in people(19) and ’Helen’ in people(19) and\
’Irene’ in people(19) and ’Jack’ in people(19) and ’Larry’in
people(19)
print ’Alice’ in people(20) and ’Frank’ in people(20) and ’Gary’ in
people(20)
print people(21) == [’Bob’]
print people(22) == [’Kelly’]
print people(23) == []
but when i wrote these lines it returns me an error
Traceback (most recent call last):
File "/Users/rafaellasavva/Desktop/people.py", line 19, in <module>
print 'Dan' in people(18) and 'Cathy' in people(18)
TypeError: argument of type 'NoneType' is not utterable
do you know what it might be wrong?
def people(age):
people=lambda age: [name for name in dic if dic[name]==age]
people(20)
[snip]
this is the code i have so far(with the help of the first post ;p). i understand how a function and a dictionary works and what I'm asked to find. but i don't get the lambda age part.
and this code doesn't give me any result
def people(age):
people=[name for name in dic if dic[name]==age]
def people(age):
people=lambda age: [name for name in dic if dic[name]==age]
but i don't get the lambda age part.
Also, your posts are acquiring the slimy stain of Google Groups, which
makes them rather distasteful. All your replies are getting
double-spaced, among other problems. Please consider switching to an
alternative newsgroup reader, or subscribing to the mailing list:
https://mail.python.org/mailman/listinfo/python-list
To the OP:
First, my apologies if my reply ends up trashing your
discussion here, but you should know what is behind Mr.
Angelico's response.
For some time now the Google Group Wars are being fought
in this group.
There is a (probably very small) clique of Google haters
who try present themselves as "the community" and who try
to intimidate anyone posting from Google Groups into using
some other means of posting, completely disregarding the
fact that for many new people or occasional posters, Google
Groups is an order of magnitude easier to use. These people
are extremely noisy and obnoxious but *do not* represent
"the community" except in their own minds. I suspect many
of them are motivated by political dislike of Google as
a corporation, or want to stay with the 1990's technology
they invested time in learning and don't want see change.
I and many other people post here from Google Groups and
you should feel free to too if it is more convenient for
you. (Of course you can also use the maillist or usenet
if you find them a good solution for *you* but please don't
feel compelled to by some loud obnoxious bullies.)
As another poster pointed out, if you are able to follow
some of the advice at,
https://wiki.python.org/moin/GoogleGroupsPython
it will help quiet down the anti-Google crowd a little but
even if you don't, those without a Google chip on their shoulder
will simply skip your posts if they find the Google formatting
too annoying. Most of us though will deal with it as adults
and try our best to answer your questions.
I just thought you should have both sides of the story so
to won't take the anti-Google crowd here as gospel.
def people(age):
people=lambda age: [name for name in dic if dic[name]==age]
but i don't get the lambda age part.
Just to explain: YBM has tried to sabotage you by posting a
solution that uses a couple of advanced Python features
(lambda and list comprehensions) that a beginner would be
unlikely to know about.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.