如何使用Python从嵌套字典提取唯一值? [英] How to extract unique values from nested dictionary with Python?

查看:418
本文介绍了如何使用Python从嵌套字典提取唯一值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢使用一个函数来列出字典中所有的值。该列表不能包含任何双重项目。该列表也必须按字母顺序排列。
我是Python的新手,我不能比使用 iteritems()函数打印字典的所有值。 p>

字典是:

  critics = {'Lisa Rose':{ 水中的女人:2.5,平局上的蛇:3.5,
我的运气:3.0,超人归来:3.5,你,我和杜普瑞:2.5,
'The Night Listener':3.0},
'Gene Seymour':{'Lady in the Water':3.0,'Snakes on a Plane':3.5,
'Just My Luck':1.5, 超人归来:5.0,夜间监听者:3.0,
'你,我和杜普瑞:3.5},
'迈克尔·菲利普斯'{水中的女人:2.5,在平面上的蛇:3.0,
'超人归来':3.5,'夜间听众':4.0},
'Claudia Puig':{'Snakes on a Plane':3.5,'Just My Luck':3.0,
'The Night Listener':4.5,'超人归来':4.0,
'你,我和Dupree':2.5},
4.0,超人回归:3.0,夜间听众:3.0,梦幻之水:3.0
'你,我和Dupree':2.0},
'杰克·马修斯:{水中的女人:3.0,蛇在平面上:4.0,
'夜听众:3.0,超人归来:5.0,你,我和杜普雷:3.5},
'Toby':{'Snakes on a Plane':4.5,'你,我和Dupree' ,超人归来:4.0}}

所以我想打印一下有被评为
喜欢:
只是我的运气;
水中的女士
飞机上的蛇
超人归来
你,我和杜普雷;



etcetera ..



有人可以帮我吗?

解决方案

最简单的方法是:

 >>> d = {1:'sadf',2:'sadf',3:'asdf'} 
>>> sort(set(d.itervalues()))
['asdf','sadf']





对于您的更新问题答案将是:

 >>> films = set()
>>>> _ = [film.update(dic)for dic in critics.itervalues()]
>>>排序(电影)
[我的运气,水中的女人,平面上的蛇,超人归来,夜间听众,你,我和杜普雷]


I like to make a function that puts out a list of all values that are in a dictionary. The list must not contain any double items. The list also has to be in alphabetical order. I'm kind of new to Python, I can't come any further than printing all the values of the dictionary with the iteritems() function.

The dictionary is:

critics={'Lisa Rose': {'Lady in the Water': 2.5, 'Snakes on a Plane': 3.5,
 'Just My Luck': 3.0, 'Superman Returns': 3.5, 'You, Me and Dupree': 2.5, 
 'The Night Listener': 3.0},
'Gene Seymour': {'Lady in the Water': 3.0, 'Snakes on a Plane': 3.5, 
 'Just My Luck': 1.5, 'Superman Returns': 5.0, 'The Night Listener': 3.0, 
 'You, Me and Dupree': 3.5}, 
'Michael Phillips': {'Lady in the Water': 2.5, 'Snakes on a Plane': 3.0,
 'Superman Returns': 3.5, 'The Night Listener': 4.0},
'Claudia Puig': {'Snakes on a Plane': 3.5, 'Just My Luck': 3.0,
 'The Night Listener': 4.5, 'Superman Returns': 4.0, 
 'You, Me and Dupree': 2.5},
'Mick LaSalle': {'Lady in the Water': 3.0, 'Snakes on a Plane': 4.0, 
 'Just My Luck': 2.0, 'Superman Returns': 3.0, 'The Night Listener': 3.0,
 'You, Me and Dupree': 2.0}, 
'Jack Matthews': {'Lady in the Water': 3.0, 'Snakes on a Plane': 4.0,
 'The Night Listener': 3.0, 'Superman Returns': 5.0, 'You, Me and Dupree': 3.5},
'Toby': {'Snakes on a Plane':4.5,'You, Me and Dupree':1.0,'Superman Returns':4.0}}

So I want to print a list of the movies that have been rated. Like: Just My Luck; Lady in the Water; Snakes on a Plane; Superman Returns; You, me and Dupree; . . . etcetera..

Can anybody help me out?

解决方案

the simplest way would be:

>>> d = {1: 'sadf', 2: 'sadf', 3: 'asdf'}
>>> sorted(set(d.itervalues()))
['asdf', 'sadf']

print it as you like.

For your update question answer would be:

>>> films = set()
>>> _ = [films.update(dic) for dic in critics.itervalues()]
>>> sorted(films)
['Just My Luck', 'Lady in the Water', 'Snakes on a Plane', 'Superman Returns', 'The Night Listener', 'You, Me and Dupree']

这篇关于如何使用Python从嵌套字典提取唯一值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆