searching lists for a string char

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2009
Posts: 3
Reputation: plzhelp is an unknown quantity at this point 
Solved Threads: 0
plzhelp plzhelp is offline Offline
Newbie Poster

searching lists for a string char

 
0
  #1
22 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
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,046
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 264
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is
 
0
  #2
21 Days Ago
Originally Posted by plzhelp View Post
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
Something like this perhaps:
  1. for idx_i, each_sublist in enumerate(main_object):
  2. for idx_j, each_string in enumerate(each_sublist):
  3. if each_string == my_letter:
  4. print 'Found %s at %s, %s' % (my_letter, idx_i, idx_j)
That could possibly work for you, otherwise you'll need to be a little more clear. Example input/output would help to define a way to solve this
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 79
Reputation: pythopian is an unknown quantity at this point 
Solved Threads: 21
pythopian pythopian is offline Offline
Junior Poster in Training

A really quick hack for more than 2 dimensions

 
1
  #3
21 Days Ago
If you want to search sequences with more than 2 levels of nesting, you could do this:

  1. def deepfind(obj, test=lambda value: True):
  2. if not isinstance(obj, (list, tuple)):
  3. if test(obj):
  4. yield obj
  5. else:
  6. for each in obj:
  7. for found in deepfind(each, test):
  8. yield found
  9.  
  10. def test():
  11. data = [
  12. ['abc', 'def', 'geh'], [
  13. ['ijk', 'lmn', 'opq'],
  14. ['rst', 'uvw', 'xyz'],
  15. ],
  16. ]
  17. print list( deepfind(data, lambda value: 'a' in value or 'z' in value) )
  18.  
  19. >>> test()
  20. ['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.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC