打印 Python 字典中的所有唯一值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17218139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:48:13  来源:igfitidea点击:

Print all Unique Values in a Python Dictionary

pythondictionary

提问by dvanaria

I'm struggling with a minor problem in Python (my program is in version 3.2.3 right now).

我在 Python 中遇到了一个小问题(我的程序现在是 3.2.3 版)。

I have a dictionary that looks like this (this is just an example, actually taken from another post here):

我有一本看起来像这样的字典(这只是一个例子,实际上取自这里的另一篇文章):

[{"abc":"movies"}, {"abc": "sports"}, {"abc": "music"}, {"xyz": "music"}, {"pqr":"music"}, {"pqr":"movies"},{"pqr":"sports"}, {"pqr":"news"}, {"pqr":"sports"}]

I want to simply print() a list of unique values, eliminating duplicates. At the end of this list, I would like to print the number of unique values in the dictionary:

我想简单地打印()一个唯一值列表,消除重复项。在此列表的末尾,我想打印字典中唯一值的数量:

movies
sports
music
news

4

Any help is appreciated. There are some other posts here I found that were somewhat related, but I don't know Python well enough to apply it to this specific problem.

任何帮助表示赞赏。我发现这里还有一些其他帖子有些相关,但我不太了解 Python,无法将其应用于这个特定问题。

回答by Martijn Pieters

Use a set():

使用set()

from itertools import chain

unique_values = set(chain.from_iterable(d.values() for d in dictionaries_list))
for value in unique_values:
    print(value)

print(len(unique_values))

Use .itervalues()on Python 2 for a little more efficiency. Because you have a listof dictionaries, we need to pull out the values from each of those dictionaries first; I used itertools.chain.from_iterable()with a generator expression to list all values in a sequence:

.itervalues()在 Python 2 上使用会提高一点效率。因为您有一个字典列表,所以我们需要先从每个字典中提取值;我使用itertools.chain.from_iterable()生成器表达式来列出序列中的所有值:

>>> for value in set(chain.from_iterable(d.values() for d in dictionaries_list)):
...     print(value)
... 
news
sports
movies
music

The alternative is to use nested loops in the generator expression:

另一种方法是在生成器表达式中使用嵌套循环:

>>> for value in set(v for d in dictionaries_list for v in d.values()):
...     print(value)
... 
news
sports
movies
music

回答by Ashwini Chaudhary

Use sethere as they only contain unique items.

set在这里使用,因为它们只包含独特的项目。

>>> lis = [{"abc":"movies"}, {"abc": "sports"}, {"abc": "music"}, {"xyz": "music"}, {"pqr":"music"}, {"pqr":"movies"},{"pqr":"sports"}, {"pqr":"news"}, {"pqr":"sports"}]
>>> s = set( val for dic in lis for val in dic.values())
>>> s 
set(['movies', 'news', 'music', 'sports'])

Loop over this set to get the expected output:

循环这个集合以获得预期的输出:

for x in s:                                
    print x
print len(s)   #print the length of set after the for-loop
... 
movies
news
music
sports
4

This line s = set( val for dic in lis for val in dic.values())is roughly equivalent to:

这一行s = set( val for dic in lis for val in dic.values())大致相当于:

s = set()
for dic in lis:
   for val in dic.values():
      s.add(val)

回答by Bala Subrahmanyam Balli

dict = {'511':'Vishnu','512':'Vishnu','513':'Ram','514':'Ram','515':'sita'}
list =[] # create empty list
for val in dict.values(): 
  if val in list: 
    continue 
  else:
    list.append(val)

print list

回答by Joe Silverstein

You can exploit the properties of setfor this purpose; each element is unique and duplicates will be ignored.

您可以set为此目的利用 的属性;每个元素都是唯一的,重复项将被忽略。

uniqueValues = set(myDict.values())

By using setin this way, duplicate elements returned from .values()will be discarded. You can then print them as follows:

通过set这种方式使用,返回的重复元素.values()将被丢弃。然后,您可以按如下方式打印它们:

for value in uniqueValues:  
    print(value)  
print(len(uniqueValues))