简体   繁体   English

python列表混乱

[英]python list confusion

hi i have a situation like this: 嗨,我有这样的情况:

>>> def get():
...     for i in range(3):
...             yield [0]
... 

and i want to get this: [0,0,0] 我想得到这个:[0,0,0]

my code now works in this way: 我的代码现在以这种方式工作:

>>> r = []
>>> r.extend(i[0] for i in get())
>>> r
[0, 0, 0]

but i don't like i[0].. some advice? 但我不喜欢i [0] ..一些建议吗?

(i'm on python3) (我在python3上)

Your code looks very strange, but I assume it's very simplified. 您的代码看起来很奇怪,但是我认为它已经非常简化。 If it's just about getting rid of the i[0] , do this: 如果只是要摆脱i[0] ,请执行以下操作:

>>> def get():
...     for i in range(3):
...             yield 0
... 
>>> r = []
>>> r.extend(get())
>>> r
[0, 0, 0]

To me, this looks like get can only ever return a list of length 1. If that's the case, drop the braces: 在我看来, get只能返回一个长度为1的列表。如果是这种情况,请删除花括号:

>>> def get():
...     for i in range(3):
...             yield 0
>>> # Or, shorter ...
>>> get = lambda: (0 for i in range(3))
>>> r = []
>>> r.extend(get())
>>> r
[0, 0, 0]

The reason you are having to use i[0] is because get() is a generator that returns a list of size 1 every time it is called. 您必须使用i[0]的原因是因为get()是一个生成器,每次调用它都会返回大小为1的list So your code i[0] for i in get() is the same as i[0] for i in ([0],[0],[0]) . 因此,您i[0] for i in get()代码i[0] for i in get()i[0] for i in ([0],[0],[0])相同。 The reason your code works is that i[0] gets the first element off the returned element which is itself the list [0] . 您的代码起作用的原因是i[0]从返回的元素本身就是list [0]获得了第一个元素。

What I gather from your question is that you want to have i for i in [0,0,0] . 我从您的问题中得到的是,您希望i for i in [0,0,0]使用i for i in [0,0,0] As mentioned in other answers this can be achieved by changing you generator to yield the int 0 instead of the list [0] . 如其他答案中所述,这可以通过将生成器更改为int 0而不是list [0] You can see the result of the generator in the following example code: 您可以在以下示例代码中查看生成器的结果:

>>> for i in get():
...   print("i={} and i[0]={}".format(i, i[0]))
... 
i=[0] and i[0]=0
i=[0] and i[0]=0
i=[0] and i[0]=0

As you can see, your generator returns a [0] every iteration and that is the reason you have to use i[0] to get the first element of each list. 如您所见,生成器在每次迭代时都返回[0] ,这就是您必须使用i[0]来获取每个列表的第一个元素的原因。

Also, since r is just the results of the generator, you can simplify by just doing the following: 另外,由于r只是生成器的结果,因此可以通过执行以下操作来简化:

>>> def gen():
...   for i in range(3):
...     yield 0
... 
>>> r = list(gen())
>>> r
[0, 0, 0]

r.extend(i[0] for i in get()) r.extend(i [0] for get()中的i)

This kind of imperative code (stateful, with inplace updates) is asking for trouble. 这种命令性代码(有状态,具有就地更新)正在引起麻烦。 That seems the canonical use for a functional flatten ( concat ): 这似乎是功能展平concat )的规范用法:

from itertools import chain

def flatten(listOfLists):
    return chain.from_iterable(listOfLists)

def get():
    for i in range(3):
        yield [0]

print(list(flatten(get())))
# [0, 0, 0]

Don't yield an array if you don't want one: 如果不需要,不要产生一个数组:

>>> def get():
...  for i in range(3):
...   yield 0
... 
>>> r = []
>>> r.extend(i for i in get())
>>> r
[0, 0, 0]

You could try this instead: 您可以尝试以下方法:

def get():
    return [0] * 3

r = [] 
r.extend(get())
r
[0, 0, 0]

??? ???

def get():
    for i in xrange(3):
        yield 0

r = list(get())

print r

or 要么

gen = (0 for i in xrange(3))

r = list(gen)

print r

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM