python - The last list elements and conditional loops -
mylist="'a','b','c'" count=0 i=0 while count< len(mylist): if mylist[i]==mylist[i+1]: print mylist[i] count +=1 +=1
error:
file "<string>", line 6, in <module> indexerror: string index out of range
i'm assuming when gets last (nth) element can't find n+1 compare to, gives me error.
interestingly, think i've done before , not had problem on larger list: here example (with credit raymond hettinger fixing up)
list=['a','a','x','c','e','e','f','f','f'] i=0 count = 0 while count < len(list)-2: if list[i] == list[i+1]: if list [i+1] != list [i+2]: print list[i] i+=1 count +=1 else: print "no" count += 1 else: +=1 count += 1
for crawling through list in way i've attempted, there fix don't go "out of range?" plan implement on large list, i'll have check if "list[i]==list[i+16]", example. in future, add on conditions "if int(mylist[i+3])-int(mylist[i+7])>10: newerlist.append[mylist[i]". it's important solve problem.
i thought inserting break statement, unsuccessful.
i know not efficient, i'm @ point it's understand best.
edit:
right, new information in op, becomes simpler. use the itertools
grouper()
recipe group data each person tuples:
import itertools def grouper(iterable, n, fillvalue=none): """collect data fixed-length chunks or blocks""" # grouper('abcdefg', 3, 'x') --> abc def gxx" args = [iter(iterable)] * n return itertools.zip_longest(*args, fillvalue=fillvalue) data = ['john', 'sally', '5', '10', '11', '4', 'john', 'sally', '3', '7', '7', '10', 'bill', 'hallie', '4', '6', '2', '1'] grouper(data, 6)
now data looks like:
[ ('john', 'sally', '5', '10', '11', '4'), ('john', 'sally', '3', '7', '7', '10'), ('bill', 'hallie', '4', '6', '2', '1') ]
which should easy work with, comparison.
old answer:
if need make more arbitrary links, rather checking continuous values:
def offset_iter(iterable, n): offset = iter(iterable) consume(offset, n) return offset data = ['a', 'a', 'x', 'c', 'e', 'e', 'f', 'f', 'f'] offset_3 = offset_iter(data, 3) item, plus_3 in zip(data, offset_3): #naturally, itertools.izip() in 2.x print(item, plus_3) #if memory usage important.
naturally, want use semantically valid names. advantage method works arbitrary iterables, not lists, , efficient , readable, without ugly, inefficient iteration index. if need continue checking once offset values have run out (for other conditions, say) use itertools.zip_longest()
(itertools.izip_longest()
in 2.x).
using the consume()
recipe itertools
.
import itertools import collections def consume(iterator, n): """advance iterator n-steps ahead. if n none, consume entirely.""" # use functions consume iterators @ c speed. if n none: # feed entire iterator zero-length deque collections.deque(iterator, maxlen=0) else: # advance empty slice starting @ position n next(itertools.islice(iterator, n, n), none)
i would, however, question if need re-examine data structure in case.
original answer:
i'm not sure aim is, gather want itertools.groupby()
:
>>> import itertools >>> data = ['a', 'a', 'x', 'c', 'e', 'e', 'f', 'f', 'f'] >>> grouped = itertools.groupby(data) >>> [(key, len(list(items))) key, items in grouped] [('a', 2), ('x', 1), ('c', 1), ('e', 2), ('f', 3)]
you can use work out when there (arbitrarily large) runs of repeated items. it's worth noting can provide itertools.groupby()
key
argument group them based on factor want, not equality.
Comments
Post a Comment