人言落日是天涯,望极天涯不见家 schrieb: > I'm a python newbie. It seems the slice operation will do copy. > for example: >>>> a = [1,2,3,4,5,6,7,8,9,0] >>>> b = a[7:] >>>> b > [8, 9, 0] >>>> a.remove(9) >>>> a > [1, 2, 3, 4, 5, 6, 7, 8, 0] >>>> b > [8, 9, 0] > > if the list have large members, the slice operations will consume many > times. > for instance, I have a long string named it as S, the size is more > than 100K > I want to parser it one part-to-part. first, I process the first 100 > byte, and pass the remainder to the next parser function. I pass the > S[100:] as an argument of the next parser function. but this operation > will cause a large bytes copy. Is there any way to just make a > reference to the remainder string not copy?
You can use itertools.islice: py> a = [1,2,3,4,5,6,7,8,9,0] py> b = itertools.islice(a, 7) py> b <itertools.islice object at 0xb7d9c34c> py> b.next() 1 py> b.next() 2 py> b.next() 3 py> b.next() 4 py> b.next() 5 py> b.next() 6 py> b.next() 7 py> b.next() Traceback (most recent call last): File "<stdin>", line 1, in ? StopIteration HTH, Martin
-- http://mail.python.org/mailman/listinfo/python-list