I have this 2 lists of list:
list1= [['user1', 186, 'Feb 2017, Apr 2017', 550, 555], ['user2', 282, 'Mai 2017', 0, 3579], ['user3', 281, 'Mai 2017', 10, 60]]
list2= [['user1', 186, 'Feb 2017, Mar 2017, Mai 2017', 0, 740],['user2', 282, 'Feb 2017', 0, 1000], ['user4', 288, 'Feb 2017', 60, 10]]
And I desire to output this:
desiredlist =[['user1', 186, 'Feb 2017, Mar 2017, Mai 2017', 550, 740], ['user2', 282, 'Feb 2017', 0, 1000], ['user3', 281, 'Mai 2017', 10, 0], ['user4', 288, 'Feb 2017', 60, 10]]
For that I have this function:
def mergesafirmacheta(list1,list2):
desiredlist = []
for id, n1, dates, n2, n3 in list1:
counter = 0
for list_2 in list2:
if n1 == list_2[1]:
desiredlist.append(list_2[:3] + [n2, list_2[4]])
else:
counter += 1
if counter == len(list2):
desiredlist.append([id, n1, dates, n2, 0])
print (desiredlist)
But this will output just this:
desiredlist =[['user1', 186, 'Feb 2017, Mar 2017, Mai 2017', 550, 740], ['user2', 282, 'Feb 2017', 0, 1000], ['user3', 281, 'Mai 2017', 10, 0]]
^^ user4 is missing (check my desired output), I can see why from my function, I try to add this after my first is statement:
elif list_2[1] != n1:
desiredlist.append(list_2)
But this won't work, this are the rules to compute the desired list:
When list2[1] == list1[1] (example 186 == 186) append to my desiredlist list2[0], list2[1], list2[2], list1[3], list2[4], if you have a list1[1] that is not in list2 lists change append to desiredlist list1[0], list1[1], list1[2], list1[3], 0, and if a list2[1] that is not in list1 lists append to desiredlist list2[0], list2[1], list2[2], list2[3], list2[4]. This is a bit hard to fallow but I think is much easier to check for my desired output and what I actually output right now.
550is taken from the first and the740is taken from the second list. But for user 2, the1000of the second list wins over the3579from the first. Why?! What is the rule?