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.

2
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by sjvr767

At first blush i would say this might do:

``````# compare these two list set and convert
# w_b into [0.5, 0, 0.5, 0]

a = ['a', 'b', 'c', 'd']
w_a = [0.25, 0.25, 0.25, 0.25]

b = ['a', 'c']
w_b = [0.5, 0.5]

for ix, aa in enumerate(a):
if aa not in b:
w_b.insert(ix, 0)

print w_b  # result --> [0.5, 0, 0.5, 0]``````

Also note that you can combine two related lists in one container:

``````# combine two lists to show their relationship better

a = ['a', 'b', 'c', 'd']
w_a = [0.25, 0.25, 0.25, 0.25]

# list of (name, weight) tuples
print zip(a, w_a)  # [('a', 0.25), ('b', 0.25), ('c', 0.25), ('d', 0.25)]

# dictioary with name:weight pairs
print dict(zip(a, w_a)) # {'a': 0.25, 'c': 0.25, 'b': 0.25, 'd': 0.25}``````

BTW, dictionaries are much faster way to look things up then indexing two separate related lists:

``````a = ['a', 'b', 'c', 'd']
w_a = [0.25, 0.25, 0.25, 0.25]

b = ['a', 'c']
w_b = [0.5, 0.5]

dict_a = dict(zip(a, w_a))
dict_b = dict(zip(b, w_b))

for k, v in dict_a.items():
if k not in dict_b.keys():
dict_b[k] = 0

print dict_b  # {'a': 0.5, 'c': 0.5, 'b': 0, 'd': 0}``````

Thank you! This works so well...

I am using your first snippet of code, the one using enumerate. While I realise that dictionary would be faster, lists fit in better with how the rest of the script is programmed....

Thank you again!