List sentences in python

I have a set of strings consisting of two sentences

a = ('What', 'happened', 'then', '?', 'What', 'would', 'you', 'like', 'to', 'drink','?') 

I tried this

 for i,j in enumerate(a): print i,j 

which gives

 0 What 1 happened 2 then 3 ? 4 What 5 would 6 you 7 like 8 to 9 drink 10 ? 

then how do i need it

 0 What 1 happened 2 then 3 ? 0 What 1 would 2 you 3 like 4 to 5 drink 6? 
+6
source share
3 answers

Would it be simplest to manually increment i instead of relying on enumerate and reset counter per character ? , . or ! .

 i = 0 for word in sentence: print i, word if word in ('.', '?', '!'): i = 0 else: i += 1 
+7
source

Too complicated. I think @JeromeJ's solution is cleaner. But:

 a=('What', 'happened', 'then', '?', 'What', 'would', 'you', 'like', 'to', 'drink','?') start = 0 try: end = a.index('?', start)+1 except: end = 0 while a[start:end]: for i,j in enumerate(a[start:end]): print i,j start = end try: end = a.index('?', start)+1 except: end = 0 
+1
source

Another:

 from itertools import chain for n,c in chain(enumerate(a[:a.index('?')+1]), enumerate(a[a.index('?')+1:])): print "{} {}".format(n,i) ....: 0 What 1 happened 2 then 3 ? 0 What 1 would 2 you 3 like 4 to 5 drink 6 ? 
+1
source

All Articles