python assignment of complex objects -
i have following code works fine. minute remove comment across statement print list(b)
fails , returns empty list. i'm thinking perhaps x getting address location of list(b)
executed part of print statement.
import itertools = [1,2,3] b = itertools.product(a,repeat=2) print str(b) #print list(b) x = list(b) print x <itertools.product object @ 0x7f5ac40a9a50> [(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3)] command took 0.03s
b
iterator. if ask list(b)
exhaust iterator, causing empty next time list(b)
.
as rule-of-thumb: when dealing iterators, need assign them names. usually, either iterate on iterator for-in
, or use list
convert iterator list.
Comments
Post a Comment