943,793 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 744
  • Python RSS
Aug 26th, 2008
0

comparing lists

Expand Post »
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!

python Syntax (Toggle Plain Text)
  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
Python Syntax (Toggle Plain Text)
  1. Insert_zero_weight(a, b, w_a, w_b)
.

Any help, whatsoever, would be appreciated.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
sjvr767 is offline Offline
7 posts
since Oct 2007
Aug 26th, 2008
0

Re: comparing lists

At first blush i would say this might do:
python Syntax (Toggle Plain Text)
  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:
python Syntax (Toggle Plain Text)
  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:
python Syntax (Toggle Plain Text)
  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.
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005
Aug 27th, 2008
0

Re: comparing lists

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!
Reputation Points: 10
Solved Threads: 1
Newbie Poster
sjvr767 is offline Offline
7 posts
since Oct 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Do we have a tool which converts python files to java
Next Thread in Python Forum Timeline: tkinter





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC