问题
I have an iterator over a mutable sequence, e.g.
foo = [1,2,3,4,5]
for bar in foo:
Is there a way to write to the elements in foo by using the reference which is contained in the iterator? The naive assignment:
bar = 42
does not work of course. Is it possible to use the "behind the curtain" reference to the sequence element which is in the iterator ?
PS: The simple solution with using an index
for i in range(len(a)):
a[i] = 42
will not work for my case, as I can't expose the container name.
回答1:
From my understanding, your use case is something like this:
class Z:
def __init__(self):
self.a, self.b, self.c = 1,2,3
def it(self):
for x in self.a, self.b, self.c:
yield x
z = Z()
for x in z.it():
if x == 1:
x = 42 # z.a should be 42? - doesn't work!
This isn't possible in python - there's no "pointer" or "reference" data type. You can work around this by yielding a setter function instead of (or along with) the value:
class Z:
def __init__(self):
self.a, self.b, self.c = 1,2,3
def it(self):
for x in 'abc':
yield getattr(self, x), lambda y: setattr(self, x, y)
z = Z()
for x, setter in z.it():
if x == 1:
setter(42) # works!
回答2:
Use enumerate() to generate indices for you in the loop:
for i, bar in enumerate(foo):
foo[i] = bar + 42
回答3:
You could use a list comprehension:
foo[:] = [expression for bar in foo]
or, if the assignment is too complicated for an expression, you could use a function, func:
foo[:] = [func(bar) for bar in foo]
If you only have an iterator, then there is no way to reassign values to the underlying container for there is no guarantee that there even is an underlying container:
def generator():
for i in range(10):
yield i
for bar in generator():
# Nothing you put here can change what's going on in generator
来源:https://stackoverflow.com/questions/19562356/python-assigning-through-an-iterator