Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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_Python 3.6_Set Comprehension - Fatal编程技术网

Python 从列表字典中获取一组唯一值

Python 从列表字典中获取一组唯一值,python,list,dictionary,python-3.6,set-comprehension,Python,List,Dictionary,Python 3.6,Set Comprehension,我有一个字典列表,其中字典也包含一个列表 我想生成相应嵌套列表的值的集,以便最终得到一组所有唯一项(在本例中为爱好) 我觉得集非常适合这样做,因为它会自动删除任何重复项,给我留下一套独特的爱好 people = [{'name': 'John', 'age': 47, 'hobbies': ['Python', 'cooking', 'reading']}, {'name': 'Mary', 'age': 16, 'hobbies': ['horses', 'cooking'

我有一个字典列表,其中字典也包含一个列表

我想生成相应嵌套列表的值的
,以便最终得到一组所有唯一项(在本例中为爱好)

我觉得
非常适合这样做,因为它会自动删除任何重复项,给我留下一套独特的爱好

people = [{'name': 'John', 'age': 47, 'hobbies': ['Python', 'cooking', 'reading']},
          {'name': 'Mary', 'age': 16, 'hobbies': ['horses', 'cooking', 'art']},
          {'name': 'Bob', 'age': 14, 'hobbies': ['Python', 'piano', 'cooking']},
          {'name': 'Sally', 'age': 11, 'hobbies': ['biking', 'cooking']},
          {'name': 'Mark', 'age': 54, 'hobbies': ['hiking', 'camping', 'Python', 'chess']},
          {'name': 'Alisa', 'age': 52, 'hobbies': ['camping', 'reading']},
          {'name': 'Megan', 'age': 21, 'hobbies': ['lizards', 'reading']},
          {'name': 'Amanda', 'age': 19, 'hobbies': ['turtles']},
          ]

unique_hobbies = (item for item in people['hobbies'] for hobby in people['hobbies'].items())

print(unique_hobbies)
这将生成一个错误:

TypeError: list indices must be integers or slices, not str
我的理解是错误的,但我不知道在哪里。我想遍历每个字典,然后遍历每个嵌套列表并将项目更新到集合中,这将删除所有重复项,给我留下一组独特的爱好。

我得到了:

people = [{'name': 'John', 'age': 47, 'hobbies': ['Python', 'cooking', 'reading']},
          {'name': 'Mary', 'age': 16, 'hobbies': ['horses', 'cooking', 'art']},
          {'name': 'Bob', 'age': 14, 'hobbies': ['Python', 'piano', 'cooking']},
          {'name': 'Sally', 'age': 11, 'hobbies': ['biking', 'cooking']},
          {'name': 'Mark', 'age': 54, 'hobbies': ['hiking', 'camping', 'Python', 'chess']},
          {'name': 'Alisa', 'age': 52, 'hobbies': ['camping', 'reading']},
          {'name': 'Megan', 'age': 21, 'hobbies': ['lizards', 'reading']},
          {'name': 'Amanda', 'age': 19, 'hobbies': ['turtles']},
          ]

unique_hobbies = (item for item in people['hobbies'] for hobby in people['hobbies'].items())

print(unique_hobbies)
unique_嗜好=set()
对于d类人群:
独特的业余爱好。更新(d['cabiods'])
印刷品(独特的爱好)

您也可以使用集合理解:

>>> unique_hobbies = {hobby for persondct in people for hobby in persondct['hobbies']}
>>> unique_hobbies
{'horses', 'lizards', 'cooking', 'art', 'biking', 'camping', 'reading', 'piano', 'hiking', 'turtles', 'Python', 'chess'}

你理解的问题是你想访问
人['cabiods']
,但是
是一个列表,只能用整数或切片索引列表。为了让它发挥作用,你需要反复浏览你的列表,然后访问每个子部分的
“嗜好”
(就像我在上面的理解集中所做的那样)。

这回答了我的问题,但如果可以的话,我还有一个跟进。它是有效的,但是PyCharm发出了一个警告,我没有得到这个警告,因为你的理解是有效的。警告是:应该是“collections.iterable”,而不是“Union[str,int,List[str]]”。不确定这是什么意思,因为
的“嗜好”
都是
列表[str]
。也许PyCharm把一些东西和其他字典条目混淆了(名字是
str
,年龄是
int
)。