Python 迭代并将第一项与字典中的所有项进行比较

Python 迭代并将第一项与字典中的所有项进行比较,python,loops,dictionary,Python,Loops,Dictionary,请帮帮我,我好像找不到办法。我正在从事一个网络科学项目,这是我使用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)

请帮帮我,我好像找不到办法。我正在从事一个网络科学项目,这是我使用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个键,子字典也大得多

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

救命啊


问题是,我仍然无法比较这两个子词典,因为我需要找到至少有两个相同电影作为一个整体的键。

您可以使用
集合。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})
要获取与每部电影相关的密钥,可以使用集合。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']})
>>从集合导入defaultdict
>>>movie_keys=defaultdict(列表)
>>>对于dic.items()中的k、v:
对于v中的电影:
电影键[movie].append(k)
...         
>>>电影钥匙
defaultdict(,{'Batman(1989):['42'],'Return of the Jedi(1983):['25','8'],'E.T.Extral Terral(1982):['42']})
字典中没有真正的“第一”项,但您可以找到包含给定电影的所有键,这些键类似于:

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], ...}

我的回答只会返回一本字典,其中包含多次观看的电影的
'title'、['offer1'、…]
对,即不是
'e.T.外星人(1982)
,而是
'return of the Jedi(1983)
。只需在解决方案中返回
重叠
,而不是字典理解的结果,就可以改变这一点

其中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\u title1
出现在与两个或多个独立的
id
键关联的值中。我们想储存

  • 观看两次或两次以上的电影的
    move\u title
  • id
    的键,与观看电影的值关联
  • 因此,我们想要一个这样的结果字典


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

    你是在寻找重叠电影的数量吗?你说的“第一”是什么意思字典中的项?字典是无序的。你想要键值最小的条目吗?好吧,不管字典的第一项是什么,都没关系。对于Blender,我一直在寻找与其他键有更多重叠电影的键。除非你使用非常旧的Python,否则使用
    collections.defaultdict会更整洁(名单)
    谢谢!但在那之后,我想知道如何比较电影标题1和电影标题2,看看它们是否都包含相同的ID,如果它们保存了,然后比较电影标题1和电影标题3,再次查看主词典中所有电影的ID。哇,好的,但现在我有了谁看过哪部电影的列表,我会我想看看字典的第一个答案,比如蝙蝠侠,然后把它和字典的第二个答案《绝地归来》进行比较,这样我就可以知道他们是否都有42个。然后对蝙蝠侠和其他人做同样的事。@Mirimari在发帖之前,请确定你想要什么作为输出,你只是在年代要求越来越多的东西ame问题。如果您有一个新问题,请将其作为一个新问题发布。对不起,我只是认为它是一样的。@Mirimari您最初的问题是关于我回答的“有多少电影重叠”,您可以随时发布一个新问题(但请先尝试自己)。;-)谢谢!但在那之后我想知道如何比较电影标题1和电影标题2,看看它们是否都包含相同的ID,如果它们保存了它们,然后比较电影标题1和电影标题3,再次查看ID,对于主词典中的所有电影标题。