2013-05-03 115 views
1

请帮忙,我似乎找不到办法做到这一点。我正在开发一个web科学项目,这是我第三个使用python的项目。将第一项与词典中的所有条目进行比较,并将其与第一项进行比较

我需要比较字典中的第一项与同一字典中的所有其他项目,但我的其他项目是字典。

例如,我有一个具有以下值的字典:

{'25': {'Return of the Jedi (1983)': 5.0}, 
'42': {'Batman (1989)': 3.0, 'E.T. the Extra-Terrestrial (1982)': 5.0}, 
'8': {'Return of the Jedi (1983)': 5.0 },'542': {'Alice in Wonderland (1951)': 3.0, 'Blade Runner (1982)': 4.0}, '7': {'Alice in Wonderland (1951)': 3.0,'Blade Runner (1982)': 4.0}} 

所以我要看看钥匙是否“25”和“42”包含相同的电影“绝地归来”,在此如果'25'和'8'具有相同的电影等等。我是他们做的,我需要知道有多少部电影重叠。

这是词典的一个例子,整个词典包含1000个键,子词典也更大。

我试着迭代,比较字典,复制,合并,加入,但我似乎无法理解我该如何做到这一点。

请帮忙!

事情是,我仍然无法比较两个子句,因为我需要找到至少有两个相同电影作为整体的键。

+0

您是否在寻找重叠的电影只是多少? – Blender 2013-05-03 04:36:31

+2

你是什么意思的第一个项目在字典中?字典是无序的。你想要最小的键值的条目? – 2013-05-03 04:37:59

+0

那么,无论字典的第一项是什么,都没关系。 – Mirimari 2013-05-03 04:54:39

回答

2

您可以使用collections.Counter

>>> dic={'25': {'Return of the Jedi (1983)': 5.0}, '42': {'Batman (1989)': 3.0, 'E.T. the Extra-Terrestrial (1982)': 5.0}, '8': {'Return of the Jedi (1983)': 5.0 }} 
>>> from collections import Counter 
>>> c=Counter(movie for v in dic.values() for movie in v) 

>>> [k for k,v in c.items() if v>1] #returns the name of movies repeated more than once 
['Return of the Jedi (1983)'] 
>>> c 
Counter({'Return of the Jedi (1983)': 2, 
     'Batman (1989)': 1, 
     'E.T. the Extra-Terrestrial (1982)': 1}) 

要获得相关的每部电影,你可以使用按键collections.defaultdict

>>> from collections import defaultdict 
>>> movie_keys=defaultdict(list) 
>>> for k,v in dic.items(): 
    for movie in v: 
     movie_keys[movie].append(k) 
...   
>>> movie_keys 
defaultdict(<type 'list'>, {'Batman (1989)': ['42'], 'Return of the Jedi (1983)': ['25', '8'], 'E.T. the Extra-Terrestrial (1982)': ['42']}) 
+0

哇,好吧,但现在我已经有谁看过哪部电影的名单,我想看看字典的第一个答案,说蝙蝠侠,并将其与该字典的第二个答案比较,回归的绝地武士,这样我就可以看到他们是否都有42个。然后为蝙蝠侠和ET做同样的事情。 – Mirimari 2013-05-03 13:43:27

+0

@Mirimari在发布问题之前,请确定你想要输出什么,你只是在同一个问题中要求越来越多的东西。如果您有新问题,请将其作为新问题发布。 – 2013-05-03 16:06:30

+0

对不起,我只是觉得它是一样的。 – Mirimari 2013-05-03 20:33:16

0

有没有真正在字典中的“第一”项目,但你可以找到所有包含给定电影的密钥,如下所示:

movies = {} 
for k in data: 
    for movie in data[k]: 
     movies.setdefault(movie, []).append(k) 

输出电影看起来像:

{'Return of the Jedi (1983)': [25, 8], 'Batman (1989)': [42], ...} 
+0

除非你使用的是一个非常古老的Python,否则使用'collections.defaultdict(list)' – 2013-05-03 06:31:27

+0

感谢!但之后我想知道如何将movie_Title1与movie_title2进行比较,看看它们是否都包含相同的ID,如果它们保存了它们,则将movie_title1与movie_title3进行比较,并再次查看ID,以查看主字典中的所有movieTitles。 – Mirimari 2013-05-03 15:21:57

0

我的答案只会返回一个包含'title',['offender1',...]双电影字典这是看到不止一次,即'E.T. the Extra-Terrestrial (1982)''Return of the Jedi (1983)'将报告。这可以通过在解决方案中简单地返回overlaps而不是字典理解的结果来改变。

其中d是:

d = {'25': {'Return of the Jedi (1983)': 5.0}, 
    '42': {'Batman (1989)': 3.0, 'E.T. the Extra-Terrestrial (1982)': 5.0}, 
    '8': {'Return of the Jedi (1983)': 5.0 }, 
    '542': {'Alice in Wonderland (1951)': 3.0, 'Blade Runner (1982)': 4.0}, 
    '7': {'Alice in Wonderland (1951)': 3.0,'Blade Runner (1982)': 4.0} 
    } 

以下:

from collections import defaultdict 
import itertools 
def findOverlaps(d): 
    overlaps = defaultdict(list) 
    for (parentKey,children) in d.items(): #children is the dictionary containing movie_title,rating pairs 
     for childKey in children.keys(): #we're only interested in the titles not the ratings, hence keys() not items() 
      overlaps[childKey].append(parentKey) #add the parent 'id' where the movie_title came from 
    return dict(((overlap,offenders) for (overlap,offenders) in overlaps.items() if len(offenders) > 1)) #return a dictionary, only if the movie title had more than one 'id' associated with it 
print(findOverlaps(d)) 

产地:

>>> 
{'Blade Runner (1982)': ['7', '542'], 'Return of the Jedi (1983)': ['25', '8'], 'Alice in Wonderland (1951)': ['7', '542']} 

代码背后的推理:

在d中的每个条目表示id : { movie_title1: rating, movie_title2: rating }。现在说movie_title1发生在与两个或多个关联单独id键。我们想要存储

  1. 该电影的move_title被看到两次或更多。
  2. id的密钥,与相关联,其中看到该电影。

因此,我们希望所得到的字典,像这样

{ move_title1: {'id1','id2'}, movie_title2: {'id2','id5'}

+0

谢谢!但之后我想知道如何将movie_Title1与movie_title2进行比较,看看它们是否都包含相同的ID,如果它们保存了它们,则将movie_title1与movie_title3进行比较,并再次查看ID,以查看主字典中的所有movieTitles。 – Mirimari 2013-05-03 15:21:39

相关问题