Split a character list based on another list Python -


i have split list of characters such gets cut when encounters vowel. example, string like

toy = ['b', 'a', 'm', 'b', 'i', 'n', 'o'] 

the output should

[('b', 'a'), ('m', 'b', 'i'), ('n', 'o')] 

i tried run 2 loops, 1 behind other.

# usr/bin/env/python  apple = [] consonants = ('k', 'h', 'b', 'n') vowels = ('i', 'a', 'u') toy = ('k', 'h', 'u', 'b', 'a', 'n', 'i') in range(len(toy)):     if == 0:         pass     else:         if toy[i] in vowels:             k in range(i - 1, len(toy)):                 if toy[k - 1] in consonants:                     n = toy[i - k:i - 1]                     apple.append(n)                     break print apple 

but not let me come out of loop once vowel reached. using "bambino" example, gives me [('b', 'a'), ('b', 'a', 'm', 'b', 'i'), ('b', 'a', 'm', 'b', 'i', 'n', 'o')]. can please help?

you seem overcomplicating things. simple solution -

>>> toy = ['b', 'a', 'm', 'b', 'i', 'n', 'o'] >>> vowels = ['a','i','e','o','u'] >>> apples = [] >>> k = 0 >>> ,x in enumerate(toy): ...     if x in vowels: ...             apples.append(tuple(toy[k:i+1])) ...             k = i+1 ... >>> apples [('b', 'a'), ('m', 'b', 'i'), ('n', 'o')] 

enumerate() function returns index current element of list.


Comments