2013-05-03 117 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

相關問題