comparing lists

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2007
Posts: 7
Reputation: sjvr767 is an unknown quantity at this point 
Solved Threads: 1
sjvr767 sjvr767 is offline Offline
Newbie Poster

comparing lists

 
0
  #1
Aug 26th, 2008
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 = ['a', 'b', 'c', 'd']
w_a = [0.25, 0.25, 0.25, 0.25]

b = ['a', 'c']
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!

  1. def comp_lists(list1, list2):
  2. length = max(len(list1), len(list2))
  3. i = 0
  4. while i < length:
  5. a = list1[i]
  6. b = list2[i]
  7. if a != b and min(a, b) != 0:
  8. break
  9. else:
  10. i = i + 1
  11. return i
  12.  
  13. def insert_zero_weight(share_list1, share_list2, w1, w2):
  14. W1 = list(w1)
  15. W2 = list(w2)
  16. if share_list1 == share_list2:
  17. return "List are equal"
  18. i = comp_lists(share_list1, share_list2)
  19. print i
  20. print max(len(share_list1), len(share_list2))
  21. print len(share_list1), len(share_list2)
  22. if i < max(len(share_list1), len(share_list2)) and len(share_list1) < len(share_list2):
  23. W1.insert(i, 0)
  24. return W1
  25. elif i < max(len(share_list1), len(share_list2)) and len(share_list1) > len(share_list2):
  26. W2.insert(i, 0)
  27. return W2
  28. else:
  29. return "Error"
  30.  
  31. def Insert_zero_weight(share_list1, share_list2, w1, w2):
  32. W1 = list(w1)
  33. W2 = list(w2)
  34. if W1 < W2:
  35. while W1 < W2:
  36. print 'in is %d long' %len(W1)
  37. W3 = insert_zero_weight(share_list1, share_list2, W1, W2)
  38. print 'out is %d long' %len(W3)
  39. del W1
  40. W1 = list(W3)
  41. return W1
  42. elif W1 > W2:
  43. while W2 < W1:
  44. print 'in is %d long' %len(W2)
  45. W3 = insert_zero_weight(share_list1, share_list2, W1, W2)
  46. print 'out is %d long' %len(W3)
  47. del W2
  48. W2 = list(W3)
  49. return W2
  50. else:
  51. return "Error"

Where you would run
  1. Insert_zero_weight(a, b, w_a, w_b)
.

Any help, whatsoever, would be appreciated.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,221
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 137
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso

Re: comparing lists

 
0
  #2
Aug 26th, 2008
At first blush i would say this might do:
  1. # compare these two list set and convert
  2. # w_b into [0.5, 0, 0.5, 0]
  3.  
  4. a = ['a', 'b', 'c', 'd']
  5. w_a = [0.25, 0.25, 0.25, 0.25]
  6.  
  7. b = ['a', 'c']
  8. w_b = [0.5, 0.5]
  9.  
  10.  
  11. for ix, aa in enumerate(a):
  12. if aa not in b:
  13. w_b.insert(ix, 0)
  14.  
  15. print w_b # result --> [0.5, 0, 0.5, 0]
Also note that you can combine two related lists in one container:
  1. # combine two lists to show their relationship better
  2.  
  3. a = ['a', 'b', 'c', 'd']
  4. w_a = [0.25, 0.25, 0.25, 0.25]
  5.  
  6. # list of (name, weight) tuples
  7. print zip(a, w_a) # [('a', 0.25), ('b', 0.25), ('c', 0.25), ('d', 0.25)]
  8.  
  9. # dictioary with name:weight pairs
  10. 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:
  1. a = ['a', 'b', 'c', 'd']
  2. w_a = [0.25, 0.25, 0.25, 0.25]
  3.  
  4. b = ['a', 'c']
  5. w_b = [0.5, 0.5]
  6.  
  7. dict_a = dict(zip(a, w_a))
  8. dict_b = dict(zip(b, w_b))
  9.  
  10. for k, v in dict_a.items():
  11. if k not in dict_b.keys():
  12. dict_b[k] = 0
  13.  
  14. print dict_b # {'a': 0.5, 'c': 0.5, 'b': 0, 'd': 0}
Last edited by bumsfeld; Aug 26th, 2008 at 11:56 am.
Should you find Irony, you can keep her!
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 7
Reputation: sjvr767 is an unknown quantity at this point 
Solved Threads: 1
sjvr767 sjvr767 is offline Offline
Newbie Poster

Re: comparing lists

 
0
  #3
Aug 27th, 2008
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!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC