| | |
comparing lists
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Oct 2007
Posts: 7
Reputation:
Solved Threads: 1
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!
Where you would run .
Any help, whatsoever, would be appreciated.
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)
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
Python Syntax (Toggle Plain Text)
Insert_zero_weight(a, b, w_a, w_b)
Any help, whatsoever, would be appreciated.
At first blush i would say this might do:
Also note that you can combine two related lists in one container:
BTW, dictionaries are much faster way to look things up then indexing two separate related lists:
python Syntax (Toggle Plain Text)
# 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]
python Syntax (Toggle Plain Text)
# 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}
python Syntax (Toggle Plain Text)
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}
Last edited by bumsfeld; Aug 26th, 2008 at 11:56 am.
Should you find Irony, you can keep her!
![]() |
Similar Threads
- Polynomials with linked lists (C++)
- comparing elemets of arraylist (Java)
- Linked Lists Help C++ (C++)
- Comparing Python and C, Part 10, Structures (Python)
- Comparing a matrix(list[][]) ? (Python)
- Compare 2 Lists of Words (MySQL)
- Double Linked Lists and Functions required (C++)
Other Threads in the Python Forum
- Previous Thread: Do we have a tool which converts python files to java
- Next Thread: tkinter
| Thread Tools | Search this Thread |
alarm ansi assignment avogadro backend beginner binary bluetooth character cmd code customdialog cx-freeze data decimals dictionary directory drive dynamic error examples exe file float format function gnu graphics gui halp heads homework http ideas import input itunes java leftmouse line linux list lists logging loop module mouse number numbers output parsing path pointer port prime programming progressbar projects push py2exe pygame pyglet pyqt python random recursion schedule screensaverloopinactive script scrolledtext sqlite statistics stdout string strings sudokusolver sum table terminal text thread threading time tkinter tlapse tricks tuple tutorial ubuntu unicode urllib urllib2 variable ventrilo webservice wikipedia windows write wxpython xlib






