Nested List Searches

Thread Solved

Join Date: Oct 2006
Posts: 2,276
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 175
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Nested List Searches

 
0
  #1
Jan 8th, 2007
I was experimenting with the nested list example in thread:
http://www.daniweb.com/techtalkforum...246791-72.html
and was trying to search a nested list like that:
  1. nested_list = [1 ,'Dick', 2, ['Mary', 7, 9, [700, 777, 'Paul']], 13]
  2.  
  3. if 'Paul' in nested_list:
  4. print 'found Paul'
  5. else:
  6. print 'Paul not found'
It always tells me that 'Paul' is not found! How come? Is there an error in Python?
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 148
Reputation: ghostdog74 is on a distinguished road 
Solved Threads: 40
ghostdog74 ghostdog74 is offline Offline
Junior Poster

Re: Nested List Searches

 
0
  #2
Jan 9th, 2007
if you just want to find "Paul" in your example, one way to do it is to convert that list to string
  1. astring = ','.join( str(i) for i in nested_list)
  2. if "Paul in astring:
  3. print "Found"
  4.  
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 133
Reputation: mawe is an unknown quantity at this point 
Solved Threads: 58
mawe mawe is offline Offline
Junior Poster

Re: Nested List Searches

 
0
  #3
Jan 9th, 2007
  1. if "x" in nested
can be written as
  1. for elem in nested: if elem == "x" ...
Your list nested_list = [1 ,'Dick', 2, ['Mary', 7, 9, [700, 777, 'Paul']], 13] consists of 5 elemenst:
1
'Dick'
2
['Mary', 7, 9, [700, 777, 'Paul']]
13

As you can see, none of these passes the test "Paul" == elem. In the 4th one (['Mary', 7, 9, [700, 777, 'Paul']]) there is the string Paul, but it is hidden in a list
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,016
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 931
Moderator
vegaseat's Avatar
vegaseat vegaseat is online now Online
DaniWeb's Hypocrite

Re: Nested List Searches

 
0
  #4
Jan 9th, 2007
You can flatten the list, might call this un-nesting and then search ...
  1. def xflatten(seq):
  2. """a generator to flatten a nested list"""
  3. for x in seq:
  4. if type(x) is list:
  5. for y in xflatten(x):
  6. yield y
  7. else:
  8. yield x
  9. nested_list = [1 ,'Dick', 2, ['Mary', 7, 9, [700, 777, 'Paul']], 13]
  10. flat_list = list(xflatten(nested_list))
  11. print flat_list # [1, 'Dick', 2, 'Mary', 7, 9, 700, 777, 'Paul', 13]
  12. if 'Paul' in flat_list:
  13. print "Found Paul in flat_list" # now it works!
If you are only searching, Ghostdog's solution is most elegant and can be simplified to ...
  1. nested_list = [1 ,'Dick', 2, ['Mary', 7, 9, [700, 777, 'Paul']], 13]
  2. nl_string = str(nested_list)
  3. print nl_string # "[1, 'Dick', 2, ['Mary', 7, 9, [700, 777, 'Paul']], 13]"
  4. if 'Paul' in nl_string:
  5. print "Found Paul in the string" # works too!
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,276
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 175
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: Nested List Searches

 
0
  #5
Jan 9th, 2007
Thank you fellow Pythonians. I learned from mawe's explanation that in order to find an item or find the maximum item you have to un-nest the list as shown by vegaseat's code.

Converting the list to a string, as Ghostdog74 suggested, works well for the search, but can't find the maximum.
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,016
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 931
Moderator
vegaseat's Avatar
vegaseat vegaseat is online now Online
DaniWeb's Hypocrite

Re: Nested List Searches

 
0
  #6
Jan 9th, 2007
Sneekula,
since you are so refreshingly inquisitive, have you tried this ...
  1. flat_list = [1, 'Dick', 2, 'Mary', 7, 9, 700, 777, 'Paul', 13]
  2. print max(flat_list)
Would you have predicted the outcome? Can you explain why?
May 'the Google' be with you!
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