Does any one know of any modules which could help me in comparing lists.
I have the following problem. There are lists with names and there are lists with associated weights. For example,
a =
w_a = [0.25, 0.25, 0.25, 0.25]
b =
w_b = [0.5, 0.5]
I need to write a function that will compare a and b and insert 0s into the appropriate positions within w_b.... i.e. the output should be a list [0.5, 0, 0.5, 0].
I have tried the following, but it goes into an endless loop!
def comp_lists(list1, list2):
length = max(len(list1), len(list2))
i = 0
while i < length:
a = list1[i]
b = list2[i]
if a != b and min(a, b) != 0:
break
else:
i = i + 1
return i
def insert_zero_weight(share_list1, share_list2, w1, w2):
W1 = list(w1)
W2 = list(w2)
if share_list1 == share_list2:
return "List are equal"
i = comp_lists(share_list1, share_list2)
print i
print max(len(share_list1), len(share_list2))
print len(share_list1), len(share_list2)
if i < max(len(share_list1), len(share_list2)) and len(share_list1) < len(share_list2):
W1.insert(i, 0)
return W1
elif i < max(len(share_list1), len(share_list2)) and len(share_list1) > len(share_list2):
W2.insert(i, 0)
return W2
else:
return "Error"
def Insert_zero_weight(share_list1, share_list2, w1, w2):
W1 = list(w1)
W2 = list(w2)
if W1 < W2:
while W1 < W2:
print 'in is %d long' %len(W1)
W3 = insert_zero_weight(share_list1, share_list2, W1, W2)
print 'out is %d long' %len(W3)
del W1
W1 = list(W3)
return W1
elif W1 > W2:
while W2 < W1:
print 'in is %d long' %len(W2)
W3 = insert_zero_weight(share_list1, share_list2, W1, W2)
print 'out is %d long' %len(W3)
del W2
W2 = list(W3)
return W2
else:
return "Error"
Where you would run
Insert_zero_weight(a, b, w_a, w_b)
.
Any help, whatsoever, would be appreciated.