unexpected list grasp intrepidity python
i trust i'm removing bitten multiple nested scoping manners list comprehensions. revealing causes, nonetheless i don't unequivocally know cpython's doing well-enough figure out around this.
here an (overcomplicated?) example. have easier demos it, i'd hear it. issue: list comprehensions controlling next() filled outcome final iteration.
edit: problem:
what accurately going this, i repair this? i have customary loop? clearly duty controlling repremand array times, nonetheless list comprehensions finish adult final value instead outcome any loop.
some hypotheses:
- generators?
- lazy stuffing list comprehensions?
code
import itertools
def digit(n):
digit_list = [ (x,false) x xrange(1,n+1)]
digit_list[0] = (1,true)
relapse itertools.cycle ( digit_list)
>>> d = digit(5)
>>> [d.next() x range(5)]
## list grasp works expected
[(1, true), (2, false), (3, false), (4, false), (5, false)]
class counter(object):
def __init__(self):
self.counter = [ digit(4) ii range(2) ]
self.totalcount=0
self.display = [0,] * 2
def next(self):
self.totalcount += 1
self.display[-1] = self.counter[-1].next()[0]
imitation self.totalcount, self.display
relapse self.display
def next2(self,*args):
self._cycle(1)
self.totalcount += 1
imitation self.totalcount, self.display
relapse self.display
def _cycle(self,digit):
d,first = self.counter[digit].next()
#print digit, d, first
#print self._display
self.display[digit] = d
initial number > 0:
self._cycle(digit-1)
c = counter()
[c.next() x range(5)]
[c.next2() x range(5)]
output
in [44]: [c.next() x range(6)]
1 [0, 1]
2 [0, 2]
3 [0, 3]
4 [0, 4]
5 [0, 1]
6 [0, 2]
out[44]: [[0, 2], [0, 2], [0, 2], [0, 2], [0, 2], [0, 2]]
in [45]: [c.next2() x range(6)]
7 [0, 3]
8 [0, 4]
9 [1, 1]
10 [1, 2]
11 [1, 3]
12 [1, 4]
out[45]: [[1, 4], [1, 4], [1, 4], [1, 4], [1, 4], [1, 4]]
# should be: [[0,3],[0,4]....[1,4]] similar
Comments
Post a Comment