944,028 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 517
  • Python RSS
Oct 19th, 2009
0

List help...

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jmark13 is offline Offline
17 posts
since Sep 2009
Oct 19th, 2009
2
Re: List help...
Using for() loops is easier. This is somewhat of a brute force method but works fine for smaller lists.
Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 741
Solved Threads: 692
Nearly a Posting Maven
woooee is offline Offline
2,307 posts
since Dec 2006
Oct 19th, 2009
0
Re: List help...
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:
python Syntax (Toggle Plain Text)
  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.
Reputation Points: 43
Solved Threads: 14
Light Poster
The_Kernel is offline Offline
37 posts
since May 2009
Oct 19th, 2009
0
Re: List help...
One of the solutions is pretty straight forward, with easy logic:
python Syntax (Toggle Plain Text)
  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.
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005
Oct 19th, 2009
0
Re: List help...
Python Syntax (Toggle Plain Text)
  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]]
Reputation Points: 14
Solved Threads: 16
Junior Poster in Training
lukerobi is offline Offline
50 posts
since Sep 2009
Oct 19th, 2009
0
Re: List help...
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.
Reputation Points: 106
Solved Threads: 35
Posting Whiz in Training
lrh9 is offline Offline
238 posts
since Oct 2009
Oct 19th, 2009
0
Re: List help...
Excellent! Thanks everyone.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jmark13 is offline Offline
17 posts
since Sep 2009
Oct 19th, 2009
1
Re: List help...
A shorter method
python Syntax (Toggle Plain Text)
  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)
Reputation Points: 930
Solved Threads: 668
Posting Maven
Gribouillis is offline Offline
2,655 posts
since Jul 2008
Oct 19th, 2009
0
Re: List help...
Thought of another way to do this:
Python Syntax (Toggle Plain Text)
  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. >>>
Reputation Points: 14
Solved Threads: 16
Junior Poster in Training
lukerobi is offline Offline
50 posts
since Sep 2009

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: calling a module from a variable?
Next Thread in Python Forum Timeline: letters to numbers





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


Follow us on Twitter


© 2011 DaniWeb® LLC