| | |
List help...
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Sep 2009
Posts: 6
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Dec 2006
Posts: 1,027
Reputation:
Solved Threads: 288
2
#2 Oct 19th, 2009
Using for() loops is easier. This is somewhat of a brute force method but works fine for smaller lists.
Python Syntax (Toggle Plain Text)
L1=[[1,2,3],[4,5,6],[7,8,9],[10,11,12]] L2=[1,2,3,4,11] L3=[] for number in L2: for a_list in L1: if (number in a_list) and (a_list not in L3): L3.append(a_list) print L3 ## ## You can also delete from the list of lists as you add it ## to L3 so you don't search through it again, but deleting ## can cause problems in some cases print "-" * 50 L1=[[1,2,3],[4,5,6],[7,8,9],[10,11,12]] L1_copy = L1[:] L2=[1,2,3,4,11] L3=[] for number in L2: for a_list in L1: if (number in a_list) and (a_list not in L3): L3.append(a_list) L1.remove(a_list) print "-----> L1 =", L1 print L3
Last edited by woooee; Oct 19th, 2009 at 1:36 am.
Linux counter #99383
•
•
Join Date: May 2009
Posts: 25
Reputation:
Solved Threads: 8
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) 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)
L1 = [[1,2,3],[4,5,6],[7,8,9],[10,11,12]] L2 = [1,2,3,4,11] for item in L2: # entry will contain all the sub lists from L1 that contain the item entry = [] for sub_list in L1: if item in sub_list: entry.append(sub_list)
Last edited by The_Kernel; Oct 19th, 2009 at 1:28 am.
0
#4 Oct 19th, 2009
One of the solutions is pretty straight forward, with easy logic:
There may be better solutions.
Sorry, woooee already had this solution!!
python Syntax (Toggle Plain Text)
list1 = [[1,2,3],[4,5,6],[7,8,9],[10,11,12]] list2 = [1,2,3,4,11] list3 = [] # q1 is each sublist in list1 for q1 in list1: # q2 is each item in list2 for q2 in list2: # do not append if sublist is already in list3 if q2 in q1 and q1 not in list3: list3.append(q1) print(list3) """my output --> [[1, 2, 3], [4, 5, 6], [10, 11, 12]] """
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!
•
•
Join Date: Sep 2009
Posts: 50
Reputation:
Solved Threads: 16
0
#5 Oct 19th, 2009
Python Syntax (Toggle Plain Text)
>>> L1 [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] >>> L2 [1, 2, 3, 4, 11] >>> L3 = [] >>> for x in L1: for y in L2: if y in x and x not in L3: L3.append(x) >>> L3 [[1, 2, 3], [4, 5, 6], [10, 11, 12]]
1
#8 Oct 19th, 2009
A shorter method
python Syntax (Toggle Plain Text)
L1=[[1,2,3],[4,5,6],[7,8,9],[10,11,12]] L2=[1,2,3,4,11] set2 = set(L2) L3 = [x for x in L1 if set2.intersection(x)] print(L3)
•
•
Join Date: Sep 2009
Posts: 50
Reputation:
Solved Threads: 16
0
#9 Oct 19th, 2009
Thought of another way to do this:
Python Syntax (Toggle Plain Text)
>>> L1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] >>> L2 = [1,2,3,4,11] >>> [y for y in L1 if [x for x in L2 if x in y] != []] [[1, 2, 3], [4, 5, 6], [10, 11, 12]] >>>
![]() |
Similar Threads
- Simple Linked List with user input (C)
- How to recursively find a value in a linked list (Java)
- A List Script? (PHP)
- Help with list 'subtraction' (Python)
- Win98SE - clearing out startup list (Windows 95 / 98 / Me)
- A List of Acronyms (Geeks' Lounge)
- Posting List (DaniWeb Community Feedback)
Other Threads in the Python Forum
- Previous Thread: Story Statistics (Python)
- Next Thread: letters to numbers
| Thread Tools | Search this Thread |
advanced aliased bash beginner bits calling casino changecolor class clear command convert corners count csv cturtle cursor def definedlines dictionary digital dynamic dynamically events examples external file float format frange function google gui hints homework i/o iframe import info input java line linux list lists loop matching mouse multiple number numbers obexftp output parsing path port prime programming projects py py2exe pygame pygtk python random rational raw_input recursion return scrolledtext signal singleton skinning stderr string strings subprocess table tails terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode urllib urllib2 valueerror variable voip web-scrape whileloop windows word wxpython






