1
我一直在研究这个问题,它似乎很长一段时间。我有一本字典,看起来像这样:比较字典中的项目字典
{'1': {'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}, '2': {'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},'3': {'Lady in the Water': 2.5, 'Snakes on a Plane': 3.0,'Superman Returns': 3.5, 'The Night Listener': 4.0}}
而实际上事情是大了很多,但我想找到的是列表或与一对共同具有至少2部电影IDS的设置与彼此。但是一定是错误的,因为第一个密钥必须与第二个密钥核对,然后第一个密钥与第三个密钥核对,直到密钥用完,然后第二个密钥用第三个密钥,依此类推,直到我没有更多密钥。那么这是第三个关键的转折点。
最后我只想得到至少有两部电影共同的钥匙。
我试着这样做:
def sim_critics(movies):
similarRaters=set()
first=1
lastCritic= ''
movie_over = collections.defaultdict(list)
movCount=Counter(movie for v in movies.values() for movie in v)
for num in movies:
for movie, _ in movies[num].items():
movie_over[movie].append(num)
for critic,_ in movie_over.items():
if first!=1:
critic_List = collections.Counter(movie_over[critic])
critic2_list = collections.Counter(movie_over[lastCritic])
overlap = list((critic_List & critic2_list).elements())
if len(overlap) >= 2:
key = critic + " and " + lastCritic
similarRaters.add(key)
lastCritic= critic
first=2
return similarRaters
谢谢!但是我有一个问题,它是否检查了前两秒和后三分之一的字典? – Mirimari 2013-05-03 23:10:07
是的,它将所有字典相互比较,并且返回语句确保它不返回与自己配对的字典。 – James 2013-05-03 23:18:26
非常感谢你!!!最后一件快速的事情,它返回1,2和2,1 ......但我认为它们是一样的。 – Mirimari 2013-05-03 23:41:10