944,201 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 4267
  • Python RSS
Jan 8th, 2007
0

Nested List Searches

Expand Post »
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:
Python Syntax (Toggle Plain Text)
  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?
Similar Threads
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Jan 9th, 2007
0

Re: Nested List Searches

if you just want to find "Paul" in your example, one way to do it is to convert that list to string
Python Syntax (Toggle Plain Text)
  1. astring = ','.join( str(i) for i in nested_list)
  2. if "Paul in astring:
  3. print "Found"
  4.  
Reputation Points: 75
Solved Threads: 44
Junior Poster
ghostdog74 is offline Offline
156 posts
since Apr 2006
Jan 9th, 2007
0

Re: Nested List Searches

Python Syntax (Toggle Plain Text)
  1. if "x" in nested
can be written as
Python Syntax (Toggle Plain Text)
  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
Reputation Points: 19
Solved Threads: 58
Junior Poster
mawe is offline Offline
133 posts
since Sep 2005
Jan 9th, 2007
0

Re: Nested List Searches

You can flatten the list, might call this un-nesting and then search ...
python Syntax (Toggle Plain Text)
  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 ...
python Syntax (Toggle Plain Text)
  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!
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Jan 9th, 2007
0

Re: Nested List Searches

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.
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Jan 9th, 2007
0

Re: Nested List Searches

Sneekula,
since you are so refreshingly inquisitive, have you tried this ...
python Syntax (Toggle Plain Text)
  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?
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

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: Tk Button Click Response
Next Thread in Python Forum Timeline: min/max of a mixed type list





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


Follow us on Twitter


© 2011 DaniWeb® LLC