Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在列表列表中搜索相似的元素_Python - Fatal编程技术网

Python 在列表列表中搜索相似的元素

Python 在列表列表中搜索相似的元素,python,Python,我有一张桌子,里面有: table = [[1, 'FANTASTIC FOUR', 'EXOTIC SPACE'],[4, 'CRIMSON PEAK', 'MINIONS','SUPERMAN'],[20, 'FANTASTIC FOUR', 'EXOTIC SPACE']] 我正在编写一个python函数来遍历表,查找字符串元素中的相似性,并以以下格式打印: Movie: FANTASTIC FOUR, EXOTIC SPACE UserID: 1,20 #since user 1

我有一张桌子,里面有:

table = [[1, 'FANTASTIC FOUR', 'EXOTIC SPACE'],[4, 'CRIMSON PEAK', 'MINIONS','SUPERMAN'],[20, 'FANTASTIC FOUR', 'EXOTIC SPACE']]
我正在编写一个python函数来遍历表,查找字符串元素中的相似性,并以以下格式打印:

Movie: FANTASTIC FOUR, EXOTIC SPACE
UserID: 1,20   #since user 1 and user 20 both watch exactly the same movie
我试着写:

i = 0
while i<len(table)-1:
    g = table[i][1:]
    if g == table[i+1][1:]:
        print(table[i][0],table[i+1][0])
    i+=1
i=0

Python中的i循环通常不太使用
i
。试试这个:

table = [[1, 'FANTASTIC FOUR', 'EXOTIC SPACE'],[4, 'CRIMSON PEAK', 'MINIONS','SUPERMAN'],[20, 'FANTASTIC FOUR', 'EXOTIC SPACE']]

watcher = {}

for x in table:
    for movie in x[1:]:
        watcher_for_movie = watcher.get(movie, [])
        watcher_for_movie.append(x[0])
        watcher[movie] = watcher_for_movie

print(watcher)
输出:

{'EXOTIC SPACE': [1, 20], 'CRIMSON PEAK': [4], 'MINIONS': [4], 'SUPERMAN': [4], 'FANTASTIC FOUR': [1, 20]}
{'FANTASTIC FOUR': [1, 20], 'EXOTIC SPACE': [1, 20], 'CRIMSON PEAK': [4], 'MINIONS': [4], 'SUPERMAN': [4]}

下面是一个使用
itertool.compositions
和字典的解决方案

使用集合或冻结集合作为字典键是最好的选择,因为无论是查找
(1,20)
还是
(20,1)
,您都希望得到相同的结果

反转此映射也很简单:

common_movies = {frozenset(v): set(k) for k, v in common.items()}

# {frozenset({'EXOTIC SPACE', 'FANTASTIC FOUR'}): {1, 20}}

您可以使用字典从
表中获取观看相同电影的用户

table = [[1, 'FANTASTIC FOUR', 'EXOTIC SPACE'],[4, 'CRIMSON PEAK', 'MINIONS','SUPERMAN'],[20, 'FANTASTIC FOUR', 'EXOTIC SPACE']]
movie_user_mapping = dict() # Create an empty dictionary

# Iterate over every item in table
for item in table:
     # Loop over the movies i.e excluding the first element
     for movie in item[1:]:
         # Check if movie is present as key in the dictionary, if not create a new key with the movie name and assign it an empty list
         if movie not in movie_user_mapping:
             movie_user_mapping[movie] = []
         # Check if user is already added to the list for the movie, if not add the user
         if item[0] not in movie_user_mapping[movie]:
             movie_user_mapping[movie].append(item[0])

# Print the result
print(movie_user_mapping)
输出:

{'EXOTIC SPACE': [1, 20], 'CRIMSON PEAK': [4], 'MINIONS': [4], 'SUPERMAN': [4], 'FANTASTIC FOUR': [1, 20]}
{'FANTASTIC FOUR': [1, 20], 'EXOTIC SPACE': [1, 20], 'CRIMSON PEAK': [4], 'MINIONS': [4], 'SUPERMAN': [4]}
“它不工作”不是一个问题描述。你得到了什么输出?“工作不太好”:这是什么意思?你能给出一个包括预期行为和实际行为的最小示例吗?