| | |
searching lists for a string char
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
0
#2 21 Days Ago
•
•
•
•
is it possible to search a multi dimensional list for a certain letter then print out where that letter is?
i know u can use list.index() to find a letter but i could figure out how to do it with multi dimensional lists.
thx in advance
python Syntax (Toggle Plain Text)
for idx_i, each_sublist in enumerate(main_object): for idx_j, each_string in enumerate(each_sublist): if each_string == my_letter: print 'Found %s at %s, %s' % (my_letter, idx_i, idx_j)
•
•
Join Date: Nov 2009
Posts: 79
Reputation:
Solved Threads: 21
If you want to search sequences with more than 2 levels of nesting, you could do this:
Python Syntax (Toggle Plain Text)
def deepfind(obj, test=lambda value: True): if not isinstance(obj, (list, tuple)): if test(obj): yield obj else: for each in obj: for found in deepfind(each, test): yield found def test(): data = [ ['abc', 'def', 'geh'], [ ['ijk', 'lmn', 'opq'], ['rst', 'uvw', 'xyz'], ], ] print list( deepfind(data, lambda value: 'a' in value or 'z' in value) ) >>> test() ['abc', 'xyz']
"I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum."
------------
This posts may be redistributed under the Creative Commons BY-SA License.
------------
This posts may be redistributed under the Creative Commons BY-SA License.
![]() |
Similar Threads
- String to char* conversion (C++)
- Using string or char in == statements (Java)
- string to char (C++)
- cannot convert parameter 1 from 'std::string' to 'char *' (C++)
- setting a string to a char (Java)
- string and char functions (C)
- converting from string to char* (C)
- Converting String* to char* (C)
Other Threads in the Python Forum
- Previous Thread: how do you make a file with content from different lists .
- Next Thread: Tasparent window inside a visible frame
| Thread Tools | Search this Thread |
abrupt ansi anti approximation assignment avogadro backend beginner binary bluetooth calculator character cmd code customdialog data decimals dictionaries dictionary directory drive dynamic error examples exe file float format function gnu graphics gui heads homework http ideas import input java launcher leftmouse line linux list lists logging loop module mouse number numbers output parsing path pointer port prime programming progressbar projects push py2exe pygame pyglet pyqt python random recursion schedule script scrolledtext sqlite statistics stdout string strings sudokusolver sum table terminal text thread threading time tlapse tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable ventrilo wikipedia windows write wxpython xlib






