当python中的元素相似时,如何在列表中有效地组合元组



我有一个格式为:的列表

mylist = [["emi", [["python", "is", "cool"], ["i", "love", "python"]]],
["jenne", [["cool", "fun", "home"]]],
["lin", [["networking", "is", "hard"], ["i", "hate", "networking"]]],
["jim", [["party"]]],
["emi", [["i", "love", "machine learning"]]],
["jenne", [["homework"]]]]

我想把类似用户的列表组合起来。换句话说,我的输出是;

myoutput = [["emi", [["python", "is", "cool"], ["i", "love", "python"], ["i", "love", "machine learning"]]],
["jenne", [["cool", "fun", "home"], ["homework"]]],
["lin", [["networking", "is", "hard"], ["i", "hate", "networking"]]],
["jim", [["party"]]]]

我使用以下代码来完成此操作。

myoutput = []
for i, item in enumerate(mylist):
mytemp = []
for sentence in item[1]:
mytemp.append(sentence)    
for j, newitem in enumerate(mylist):
if i != j:
if item[0] == newitem[0]:
for sent in newitem[1]:
mytemp.append(sent)

但是,我的代码是O(n(^2。我认为在python中有更有效的方法来处理这个问题。请让我知道你的想法,以提高效率。

如果需要,我很乐意提供更多细节。

我们首先将整个列表读取到dict中。之后,我们只需将dict转换回列表列表。

还要注意下面的if条件。如果用户的名字已经在dict中,我们只需将其附加到dict 中的列表中

mylist = [["emi", [["python", "is", "cool"], ["i", "love", "python"]]],
["jenne", [["cool", "fun", "home"]]],
["lin", [["networking", "is", "hard"], ["i", "hate", "networking"]]],
["jim", [["party"]]],
["emi", [["i", "love", "machine learning"]]],
["jenne", [["homework"]]]]
def convert():
d = {}
for user in mylist:
if user[0] in d: # Condition that user is already present.
for arr in user[1]:
d[user[0]].append(arr)
continue
d[user[0]] = user[1] # User not present. Therefore added a key in the dict
ans = [[k,v] for k,v in d.items()] # Converting dict to list of lists
print(ans)
return ans

#OUTPUT
[['emi', [['python', 'is', 'cool'], ['i', 'love', 'python'], ['i', 'love', 'machine learning']]],
['jenne', [['cool', 'fun', 'home'], ['homework']]],
['lin', [['networking', 'is', 'hard'], ['i', 'hate', 'networking']]],
['jim', [['party']]]]

最新更新