| | |
Nested List Searches
Thread Solved |
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:
It always tells me that 'Paul' is not found! How come? Is there an error in Python?
http://www.daniweb.com/techtalkforum...246791-72.html
and was trying to search a nested list like that:
Python Syntax (Toggle Plain Text)
nested_list = [1 ,'Dick', 2, ['Mary', 7, 9, [700, 777, 'Paul']], 13] if 'Paul' in nested_list: print 'found Paul' else: print 'Paul not found'
No one died when Clinton lied.
•
•
Join Date: Apr 2006
Posts: 148
Reputation:
Solved Threads: 40
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)
astring = ','.join( str(i) for i in nested_list) if "Paul in astring: print "Found"
•
•
Join Date: Sep 2005
Posts: 133
Reputation:
Solved Threads: 58
Python Syntax (Toggle Plain Text)
if "x" in nested
Python Syntax (Toggle Plain Text)
for elem in nested: if elem == "x" ...
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
You can flatten the list, might call this un-nesting and then search ...
If you are only searching, Ghostdog's solution is most elegant and can be simplified to ...
python Syntax (Toggle Plain Text)
def xflatten(seq): """a generator to flatten a nested list""" for x in seq: if type(x) is list: for y in xflatten(x): yield y else: yield x nested_list = [1 ,'Dick', 2, ['Mary', 7, 9, [700, 777, 'Paul']], 13] flat_list = list(xflatten(nested_list)) print flat_list # [1, 'Dick', 2, 'Mary', 7, 9, 700, 777, 'Paul', 13] if 'Paul' in flat_list: print "Found Paul in flat_list" # now it works!
python Syntax (Toggle Plain Text)
nested_list = [1 ,'Dick', 2, ['Mary', 7, 9, [700, 777, 'Paul']], 13] nl_string = str(nested_list) print nl_string # "[1, 'Dick', 2, ['Mary', 7, 9, [700, 777, 'Paul']], 13]" if 'Paul' in nl_string: print "Found Paul in the string" # works too!
May 'the Google' be with you!
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.
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.
Sneekula,
since you are so refreshingly inquisitive, have you tried this ...
Would you have predicted the outcome? Can you explain why?
since you are so refreshingly inquisitive, have you tried this ...
python Syntax (Toggle Plain Text)
flat_list = [1, 'Dick', 2, 'Mary', 7, 9, 700, 777, 'Paul', 13] print max(flat_list)
May 'the Google' be with you!
![]() |
Similar Threads
- Starting Python (Python)
- please help me: build a binary search tree by Lisp (Legacy and Other Languages)
- Client Server Interaction (Python)
- Speed Up Those Searches Now! (Windows tips 'n' tweaks)
Other Threads in the Python Forum
- Previous Thread: Tk Button Click Response
- Next Thread: min/max of a mixed type list
| Thread Tools | Search this Thread |
alarm app beginner cipher cmd coordinates cx-freeze data decimals development dictionary directory dynamic error examples feet file float format function generator getvalue gui halp homework http images import input ip itunes java keycontrol leftmouse line linux list lists loop maintain maze millimeter module mouse mysqldb number numbers output parsing path port prime programming projects push py2exe pygame pyglet pymailer pyqt python queue random recursion schedule screensaverloopinactive script scrolledtext slicenotation split sqlite ssh string strings sudokusolver table terminal text thread threading time tlapse tuple tutorial ubuntu unicode url urllib urllib2 variable variables ventrilo verify vigenere web webservice wikipedia wx.wizard wxpython xlwt






