List help...

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

Join Date: Sep 2009
Posts: 6
Reputation: jmark13 is an unknown quantity at this point 
Solved Threads: 0
jmark13 jmark13 is offline Offline
Newbie Poster

List help...

 
0
  #1
Oct 19th, 2009
So the Goal is: given a Nested list that contains 3 elements in each element in the nest, i.e. L1=[[1,2,3],[4,5,6],[7,8,9],[10,11,12]] how can I take a flat list, i.e. L2=[1,2,3,4,11] where if the elements within the nested list, i.e. L1[0], L1[1], etc. intersect with any single element in L2, the element within the nested list is returned into a different list, L3

OK, here's what I have:
L1=[[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
L2=[1,2,3,4,11]
L3=[]
u=0
s=0
while s<=len(L2):
if L2[s] == L1[0][0]:
L1.pop(0)
if L2[s] == L1[0][1]:
L1.pop(0)
if L2[s] == L1[0][2]:
L1.pop(0)
else:
listy=L2[s+1]
while u<=JDAWGlen:
if listy == L1[u][0]:
L1.pop(u)
if listy == L1[u][1]:
L1.pop(u)
if listy == L1[u][2]:
L1.pop(u)
else: break
u+=1
s+=1


But this is where I freeze. How can I get the "popped" lists into a list L3? L3 should be L3=[[1,2,3],[4,5,6],[10,11,12]]

Maybe there is a much faster and better way to go about this- maybe by using a "for" loop. Or maybe there is a much much better way with a dictionary, but I have no idea how to use it for this. so step by step instructions would be helpful.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,035
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 290
woooee woooee is offline Offline
Veteran Poster
 
2
  #2
Oct 19th, 2009
Using for() loops is easier. This is somewhat of a brute force method but works fine for smaller lists.
  1. L1=[[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
  2. L2=[1,2,3,4,11]
  3. L3=[]
  4.  
  5. for number in L2:
  6. for a_list in L1:
  7. if (number in a_list) and (a_list not in L3):
  8. L3.append(a_list)
  9. print L3
  10. ##
  11. ## You can also delete from the list of lists as you add it
  12. ## to L3 so you don't search through it again, but deleting
  13. ## can cause problems in some cases
  14. print "-" * 50
  15. L1=[[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
  16. L1_copy = L1[:]
  17. L2=[1,2,3,4,11]
  18. L3=[]
  19. for number in L2:
  20. for a_list in L1:
  21. if (number in a_list) and (a_list not in L3):
  22. L3.append(a_list)
  23. L1.remove(a_list)
  24. print "-----> L1 =", L1
  25. print L3
Last edited by woooee; Oct 19th, 2009 at 1:36 am.
Linux counter #99383
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 25
Reputation: The_Kernel is an unknown quantity at this point 
Solved Threads: 8
The_Kernel The_Kernel is offline Offline
Light Poster
 
0
  #3
Oct 19th, 2009
Here's how I would approach this:

(1) for each entry in L2 find all the matching sub-lists from L1. You can use a list comprehension or a for loop for this.
(2) Add all the sub-lists that matched into a single list
(3) Reduce the list containing all the sub-lists to just the unique entries.

To get you started here's the code for the first step using a for loop:
  1. L1 = [[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
  2. L2 = [1,2,3,4,11]
  3.  
  4. for item in L2:
  5. # entry will contain all the sub lists from L1 that contain the item
  6. entry = []
  7. for sub_list in L1:
  8. if item in sub_list:
  9. entry.append(sub_list)
Last edited by The_Kernel; Oct 19th, 2009 at 1:28 am.
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
 
0
  #4
Oct 19th, 2009
One of the solutions is pretty straight forward, with easy logic:
  1. list1 = [[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
  2. list2 = [1,2,3,4,11]
  3.  
  4. list3 = []
  5. # q1 is each sublist in list1
  6. for q1 in list1:
  7. # q2 is each item in list2
  8. for q2 in list2:
  9. # do not append if sublist is already in list3
  10. if q2 in q1 and q1 not in list3:
  11. list3.append(q1)
  12.  
  13. print(list3)
  14.  
  15. """my output -->
  16. [[1, 2, 3], [4, 5, 6], [10, 11, 12]]
  17. """
There may be better solutions.
Sorry, woooee already had this solution!!
Last edited by bumsfeld; Oct 19th, 2009 at 1:44 am.
Should you find Irony, you can keep her!
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 50
Reputation: lukerobi is an unknown quantity at this point 
Solved Threads: 16
lukerobi lukerobi is offline Offline
Junior Poster in Training
 
0
  #5
Oct 19th, 2009
  1. >>> L1
  2. [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
  3. >>> L2
  4. [1, 2, 3, 4, 11]
  5. >>> L3 = []
  6. >>> for x in L1:
  7. for y in L2:
  8. if y in x and x not in L3:
  9. L3.append(x)
  10.  
  11.  
  12. >>> L3
  13. [[1, 2, 3], [4, 5, 6], [10, 11, 12]]
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 73
Reputation: lrh9 is an unknown quantity at this point 
Solved Threads: 10
lrh9 lrh9 is online now Online
Junior Poster in Training
 
0
  #6
Oct 19th, 2009
Is anyone else besides me thinking Python sets might be a good tool to use to solve this?

Edit: Actually this probably won't work. According to Python documentation on sets, sets are containers for unique objects.
Last edited by lrh9; Oct 19th, 2009 at 2:20 am. Reason: Quick addendum.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 6
Reputation: jmark13 is an unknown quantity at this point 
Solved Threads: 0
jmark13 jmark13 is offline Offline
Newbie Poster
 
0
  #7
Oct 19th, 2009
Excellent! Thanks everyone.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 948
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 217
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark
 
1
  #8
Oct 19th, 2009
A shorter method
  1. L1=[[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
  2. L2=[1,2,3,4,11]
  3.  
  4. set2 = set(L2)
  5. L3 = [x for x in L1 if set2.intersection(x)]
  6. print(L3)
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 50
Reputation: lukerobi is an unknown quantity at this point 
Solved Threads: 16
lukerobi lukerobi is offline Offline
Junior Poster in Training
 
0
  #9
Oct 19th, 2009
Thought of another way to do this:
  1. >>> L1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
  2. >>> L2 = [1,2,3,4,11]
  3. >>> [y for y in L1 if [x for x in L2 if x in y] != []]
  4. [[1, 2, 3], [4, 5, 6], [10, 11, 12]]
  5. >>>
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC