python - Search a list using a string -
i have list of lists:
= [['andy', 'dear', 'boy', 'tobe', 'todo'], ['where', 'what', 'when', 'how'], ['korea', 'japan', 'china', 'usa'], ['tweet', 'where', 'why', 'how']]
i have three questions exact:
- how retrieve sub-list list using particular element keyword?
instance, want retrieve lists having element 'why' in them? is
best possible way of doing so? - how retrieve sub-list list using part of particular element keyword?
instance, want retrieve lists having elements containing 'wh' beginning
characters of of elements? - how position or index of resulting sub-lists of these 2 searching methods?
i familiar concept of retrieving elements list matching particular keyword, confusing when comes retrieve lists matching particular keyword...
any guesses? in advance.
simple , straight
elem = 'why' index_li = [] idx, item in enumerate(a): word in item: if word.startswith(elem): index_li.append(idx) break print index_li
example
>>> elem = 'wh' ... index_li = [] ... idx, item in enumerate(a): ... word in item: ... if word.startswith(elem): ... print word, item ... index_li.append(idx) ... break ... print index_li ['where', 'what', 'when', 'how'] ['tweet', 'where', 'why', 'how'] [1, 3]
Comments
Post a Comment