Can't iterate through a string and collect a list of strings in python. Typerror? -
i'm looking able gather list of strings divides larger string marker ',
however, maintain getting error:
typeerror: string indices must integers, not str
here's code if take @ it
for f in phonebook: print f if phonebook[f] + phonebook[f+1] == "'," : linestring = phonebook[startpoint:(f+1)] arrayofstrings[j] = linestring startpoint = f+2 j = j+1 #iterate through array homecoming arrayofstrings
a sample of how want code in end like:
print arrayofstrings[1]
+445557284
print arrayofstrings[4]
+445558928 etc.
every f
going 1 of strings in list phonebook
. it's list, needs indexed integers, can see.
what want enumerate
for idx, val in enumerate(phonebook): if phonebook[idx] + phonebook[idx+1] == "',"
you should create sure check bounds, otherwise you're going run on end of list here!
there recipe pairwise
implment:
from itertools import tee, izip def pairwise(iterable): "s -> (s0,s1), (s1,s2), (s2, s3), ..." a, b = tee(iterable) next(b, none) homecoming izip(a, b) a, b in pairwise(phonebook): if + b == "',":
python string int typeerror
No comments:
Post a Comment