根据字典值进行设定 [英] Making a set from dictionary values
本文介绍了根据字典值进行设定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想根据现有字典的值创建一个集合
I want to create a set from the values of an existing dict
def function(dictionary):
...
return set_of_values
说我的字典看起来像这样:{'1': 'Monday', '2': 'Tuesday', '3': 'Monday'}
Say my dictionary looks like this: {'1': 'Monday', '2': 'Tuesday', '3': 'Monday'}
我希望返回一个仅包含唯一值的集合,如下所示:
I want a set returned that only contains the unique values, like this:
{'Monday', 'Tuesday'}
推荐答案
对于Python:
set(d.values())
等效于Python 2.7:
Equivalent on Python 2.7:
set(d.viewvalues())
如果您需要交叉兼容的Python 2.7/3.x代码:
If you need a cross-compatible Python 2.7/3.x code:
{d[k] for k in d}
这篇关于根据字典值进行设定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文