Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python从字典中检索所有项_Python_List_Dictionary - Fatal编程技术网

Python从字典中检索所有项

Python从字典中检索所有项,python,list,dictionary,Python,List,Dictionary,我把两个列表合并成一本词典。现在我想返回与某个年龄相关的所有名字。当我在下面运行时,我得到一个错误,显示在下面的输出中。如何使用函数“people”中的第一个函数中的字典 names = ['Katie', 'Bill', 'Cate', 'David', 'Erwin', 'Franz', 'Rich', 'Heath', 'Ivan', 'Justin', 'Kelly', 'Lauren'] ages = [17, 21, 17, 17, 19, 21, 20, 20, 19, 19, 2

我把两个列表合并成一本词典。现在我想返回与某个年龄相关的所有名字。当我在下面运行时,我得到一个错误,显示在下面的输出中。如何使用函数“people”中的第一个函数中的字典

names = ['Katie', 'Bill', 'Cate', 'David', 'Erwin', 'Franz', 'Rich', 'Heath', 'Ivan', 'Justin', 'Kelly', 'Lauren']
ages = [17, 21, 17, 17, 19, 21, 20, 20, 19, 19, 22, 19]

def combine_lists_into_dict():
    dictionary = dict(zip(names,ages))
    return dictionary
print combine_lists_into_dict()

def people(ages):
    dictionary.get(ages, default = None)
    return names
print people('20')
输出:

{'Lauren': 19, 'Justin': 19, 'Kelly': 22, 'Cate': 17, 'Bill': 21, 'David': 17, 'Erwin': 19, 'Ivan': 19, 'Rich': 20, 'Franz': 21, 'Heath': 20, 'Katie': 17}

Traceback (most recent call last):
  line 50, in <module>
    print people('20')
  line 48, in people
    dictionary.get(ages, default = None)
NameError: global name 'dictionary' is not defined
['Rich', 'Heath']
{'Lauren':19,'Justin':19,'Kelly':22,'Cate':17,'Bill':21,'David':17,'Erwin':19,'Ivan':19,'Rich':20,'Franz':21,'Heath':20,'Katie':17}
回溯(最近一次呼叫最后一次):
第50行,在
印刷人物('20')
第48行,人
get(年龄,默认值=无)
NameError:未定义全局名称“dictionary”

你不需要编字典就能得到你想要的名字。使用:


如果要使用字典(以及生成字典的函数),请指定函数的返回值:

>>> def combine_lists_into_dict():
...     dictionary = dict(zip(names,ages))
...     return dictionary
... 
>>> dictionary = combine_lists_into_dict()
>>> [name for name, age in dictionary.items() if age == 20]
['Rich', 'Heath']
顺便说一句,
20
'20'
是不同的。通过
20
而不是
'20'

>>> 20 == '20'
False
三个问题:

  • dictionary
    是一个局部变量,因此
    people
    函数无法看到它,因此未定义
    全局名称“dictionary”
  • .get()
    按键查找,而不是按值查找,因此它不会返回您想要的内容
  • 当年龄为整数时,传递年龄的字符串
    '20'
  • 要解决#1,请将从
    combine_lists_into_dict
    返回的字典分配给变量,并将该变量传递给需要它的函数

    要解决#2,请使用列表理解返回与年龄匹配的姓名

    对于#3,传递一个整数

    names = ['Katie', 'Bill', 'Cate', 'David', 'Erwin', 'Franz', 'Rich', 'Heath', 'Ivan', 'Justin', 'Kelly', 'Lauren']
    ages = [17, 21, 17, 17, 19, 21, 20, 20, 19, 19, 22, 19]
    
    def combine_lists_into_dict():
        dictionary = dict(zip(names,ages))
        return dictionary
    
    def people(D,age):
        return [key for key,value in D.items() if value == age]
    
    D = combine_lists_into_dict()
    print people(D,20)
    
    输出:

    {'Lauren': 19, 'Justin': 19, 'Kelly': 22, 'Cate': 17, 'Bill': 21, 'David': 17, 'Erwin': 19, 'Ivan': 19, 'Rich': 20, 'Franz': 21, 'Heath': 20, 'Katie': 17}
    
    Traceback (most recent call last):
      line 50, in <module>
        print people('20')
      line 48, in people
        dictionary.get(ages, default = None)
    NameError: global name 'dictionary' is not defined
    
    ['Rich', 'Heath']
    

    明白了,但你不能把一个函数传递到下一个函数?对不起,这是我第一次和你一起工作dictionaries@karmesto,可以传递函数(函数是Python中的对象)。也可以在函数内部调用它
    dictionary=将列表合并成dict()
    我明白了,很好。谢谢你的帮助/解释