python - Given a word from a list, I need to find all the next number values of the list -


i starting python. sorry if question trivial.

i have been searching, not found problem.

given word list, need find first word contains @ least 1 number position of given word.

for example, given word = car , list is:

ls1 = ['moto', 'frezze', 'car', 'deciding', 'local', 'using', '4587125', 'joy', 'car', 'yort', '548h21'] 

i expected return:

ls2 = ['4587125','548h21'] 

i have been trying, no results found... help,

my code,

def hasnumbers(inputstring):  return any(char.isdigit() char in inputstring)  def number_word (character):   while not hasnumbers(character):     character = [ls1()[i + 1] i, word in enumerate(ls1()[:-1]) if word == character]     if hasnumbers(character):         return character 

marcus

iterate list , keep track how have seen word, when see number, yield have seen word before. can make generator:

def get_nums_after(lst, word):     seen = 0     x in lst:         if x == word:             seen += 1         if any(c.isdigit() c in x):             while seen > 0:                 yield x                 seen -= 1 

examples:

ls1 = ['moto', 'frezze', 'car', 'deciding', 'local', 'using', '4587125', 'joy', 'car', 'yort', '548h21'] print(list(get_nums_after(ls1, "car"))) # ['4587125', '548h21'] ls2 = ["1", "car", "foo", "2", "3", "car", "car", "4"] print(list(get_nums_after(ls2, "car"))) # ['2', '4', '4'] 

Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -