Reputation: 4618
I have list in the format of:
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"]]]]
I want to combine the lists of similar users. In other words, my output is;
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"]]]]
I use the following code to do this.
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)
However, my code is O(n)^2. I think there is more efficient way of handling this in python. Please let me know your thoughts to make this more efficient.
I am happy to provide more details if needed.
Upvotes: 0
Views: 53
Reputation: 2584
We first read the entire list of lists into a dict. After that we merely convert the dict back into a list of lists.
Also note the if condition below. If the name of the user is already in the dict, we just append to the list in the 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']]]]
Upvotes: 3