Search a list using a string

I have a list of lists:

A = [['andy', 'dear', 'boy', 'tobe', 'todo'], ['where', 'what', 'when', 'how'], ['korea', 'japan', 'china', 'usa'], ['tweet', 'where', 'why', 'how']] 

I have three questions:

  • How to get a list from this list using a specific element as a keyword? For example, I want to get all lists with an why element in them? What is the best way to do this?
  • How to get a list from this list using part of a specific element as a keyword?
    For example, I want to get all lists containing elements containing "wh", starting with the characters of any of the elements?
  • How to get the position or index of subscriptions received from any of these two search methods?

I am familiar with the concept of extracting all elements from a list with a match for a particular keyword, but it confuses him when it comes to getting all lists matching a particular keyword ...

Any guesses? Thanks in advance.

+6
source share
8 answers

Simple and direct

 elem = 'why' index_li = [] for idx, item in enumerate(A): for word in item: if word.startswith(elem): index_li.append(idx) break print index_li 

Example

 >>> elem = 'wh' ... index_li = [] ... for idx, item in enumerate(A): ... for word in item: ... if word.startswith(elem): ... print word, item ... index_li.append(idx) ... break ... print index_li where ['where', 'what', 'when', 'how'] where ['tweet', 'where', 'why', 'how'] [1, 3] 
+6
source

For all answers in combination:

 mylist = [['andy', 'dear', 'boy', 'tobe', 'todo'], ['where', 'what', 'when', 'how'], ['korea', 'japan', 'china', 'usa'], ['tweet', 'where', 'why', 'how']] for num, sublist in enumerate(mylist): if 'why' in sublist: print sublist for ele in sublist: if ele.startswith('wh'): print ele, num 
+3
source

First up :

 >>> for l in A: ... if 'why' in l: ... print l ... ['tweet', 'where', 'why', 'how'] 

For second: (wy any where)

 >>> for l in A: ... for i in l: ... if 'wh' in i: ... print l ... break ... ['where', 'what', 'when', 'how'] ['tweet', 'where', 'why', 'how'] 

for testing at the beginning try this: (using startswith () from @Harido)

 >>> for l in A: ... for i in l: ... if i.startswith('wh'): ... print l ... break ... ['where', 'what', 'when', 'how'] ['tweet', 'where', 'why', 'how'] 

For the third:

To find the index, you can use the A.index(l) method after printing the stamens, for example:

 >>> for l in A: ... for i in l: ... if 'wh' in i: ... print l ... print A.index(l) ... break ... ['where', 'what', 'when', 'how'] 1 ['tweet', 'where', 'why', 'how'] 3 

But remember that I am not good at Python. Some may give you better ways. (I write C as bad code) I would like to share this link: Guido van Rossum

Edit :

thanks @Jaime, suggesting me for k, l in enumerate(A):

 >>> for k, l in enumerate(A): ... for i in l: ... if 'wh' in i: ... print l ... print "index =", k ... break ... ['where', 'what', 'when', 'how'] index = 1 ['tweet', 'where', 'why', 'how'] index = 3 
+2
source

I love lists and functional programming, so here's the approach

A

 sub_list_a = [ el for el in A if 'why' in el ] 

B

 sub_list_b = [ el for el in A if any( ['wh' in s for s in el] ) ] 

A little harder to read, but concise and reasonable.

WITH

Then, to get the indexes where each of the sub_lists was found

 location_a = [ ii for ii, el in enumerate(A) if el in sub_list_a ] 

Just replace b with a to get space for part b.

+2
source

You would do it the same way as for one list, except that you would need an extra loop:

 for inner_list in A: for element in inner_list: if # <some condition> # <do something> 

For items, adding enumerate to the specific loop that you want the item to help, or just using something like find for each internal list, will also do the job.

0
source

a. Suppose you have a list A and the word you are looking for is x.

 def howmuch(word) lis = [] for x in A: //point A if x.count(word) > 0: //point A lis.append(x) return lis 

Basically lis is a list containing all the lists that have this word in it. you iterate over the original list. each x element is a list. x.count (word) tells you how many times this word is in this list. 0 means that he never appeared on the list. therefore, if it is greater than 0, it should be in the list. if so, we added lis to our variable. then we will return it.

b. this is the same as problem a with the exception of point A, after you get the list, do another loop for each word in it. for python there is a find function for strings to see if a substring exists there, if it exists, and then add it to the list item:

http://docs.python.org/2/library/string.html

with. for problem a, after you check that the counter is greater than 0, it means that the word exists in the list. execute list.index (word) and it will return the index. for task b, as soon as you find the line with the corresponding substring, execute list.index (word).

In general, you should look at the python site, it will tell you about many functions that you can use.

0
source
 >>> A = [['andy', 'dear', 'boy', 'tobe', 'todo'], ['where', 'what', 'when', 'how'], ['korea', 'japan', 'china', 'usa'], ['tweet', 'where', 'why', 'how']] >>> for i, sublist in enumerate(A): if 'why' in sublist: print i, sublist 3 ['tweet', 'where', 'why', 'how'] >>> for i, sublist in enumerate(A): if any(word.startswith('wh') for word in sublist): print i, sublist 1 ['where', 'what', 'when', 'how'] 3 ['tweet', 'where', 'why', 'how'] 
0
source

You can use this simple approach:

 >>> key='wh' >>> res=[(index, sublist) for index, sublist in enumerate(A) for value in sublist if value.startswith(key)] >>> dict(res) {1: ['where', 'what', 'when', 'how'], 3: ['tweet', 'where', 'why', 'how']} 

enjoy it!

0
source

All Articles