问题:Python使用枚举内部列表理解
假设我有一个这样的列表:
mylist = ["a","b","c","d"]
要获得打印的值及其索引,我可以使用Python的enumerate
函数,如下所示
>>> for i,j in enumerate(mylist):
... print i,j
...
0 a
1 b
2 c
3 d
>>>
现在,当我尝试在A中使用它时,出现list comprehension
了这个错误
>>> [i,j for i,j in enumerate(mylist)]
File "<stdin>", line 1
[i,j for i,j in enumerate(mylist)]
^
SyntaxError: invalid syntax
所以,我的问题是:在列表理解中使用枚举的正确方法是什么?
Lets suppose I have a list like this:
mylist = ["a","b","c","d"]
To get the values printed along with their index I can use Python’s enumerate
function like this
>>> for i,j in enumerate(mylist):
... print i,j
...
0 a
1 b
2 c
3 d
>>>
Now, when I try to use it inside a list comprehension
it gives me this error
>>> [i,j for i,j in enumerate(mylist)]
File "<stdin>", line 1
[i,j for i,j in enumerate(mylist)]
^
SyntaxError: invalid syntax
So, my question is: what is the correct way of using enumerate inside list comprehension?
回答 0
试试这个:
[(i, j) for i, j in enumerate(mylist)]
您需要放入i,j
一个元组以使列表理解起作用。另外,鉴于enumerate()
已经返回一个元组,您可以直接将其返回而无需先拆包:
[pair for pair in enumerate(mylist)]
无论哪种方式,返回的结果都是预期的:
> [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd')]
Try this:
[(i, j) for i, j in enumerate(mylist)]
You need to put i,j
inside a tuple for the list comprehension to work. Alternatively, given that enumerate()
already returns a tuple, you can return it directly without unpacking it first:
[pair for pair in enumerate(mylist)]
Either way, the result that gets returned is as expected:
> [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd')]
回答 1
只需明确一点,这enumerate
与列表理解语法无关。
此列表推导返回一个元组列表:
[(i,j) for i in range(3) for j in 'abc']
这是字典列表:
[{i:j} for i in range(3) for j in 'abc']
列表清单:
[[i,j] for i in range(3) for j in 'abc']
语法错误:
[i,j for i in range(3) for j in 'abc']
这是不一致的(IMHO),并且与字典理解语法混淆:
>>> {i:j for i,j in enumerate('abcdef')}
{0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e', 5: 'f'}
和一组元组:
>>> {(i,j) for i,j in enumerate('abcdef')}
set([(0, 'a'), (4, 'e'), (1, 'b'), (2, 'c'), (5, 'f'), (3, 'd')])
正如ÓscarLópez所述,您可以直接通过枚举元组:
>>> [t for t in enumerate('abcdef') ]
[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e'), (5, 'f')]
Just to be really clear, this has nothing to do with enumerate
and everything to do with list comprehension syntax.
This list comprehension returns a list of tuples:
[(i,j) for i in range(3) for j in 'abc']
this a list of dicts:
[{i:j} for i in range(3) for j in 'abc']
a list of lists:
[[i,j] for i in range(3) for j in 'abc']
a syntax error:
[i,j for i in range(3) for j in 'abc']
Which is inconsistent (IMHO) and confusing with dictionary comprehensions syntax:
>>> {i:j for i,j in enumerate('abcdef')}
{0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e', 5: 'f'}
And a set of tuples:
>>> {(i,j) for i,j in enumerate('abcdef')}
set([(0, 'a'), (4, 'e'), (1, 'b'), (2, 'c'), (5, 'f'), (3, 'd')])
As Óscar López stated, you can just pass the enumerate tuple directly:
>>> [t for t in enumerate('abcdef') ]
[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e'), (5, 'f')]
回答 2
或者,如果您不坚持使用列表理解:
>>> mylist = ["a","b","c","d"]
>>> list(enumerate(mylist))
[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd')]
Or, if you don’t insist on using a list comprehension:
>>> mylist = ["a","b","c","d"]
>>> list(enumerate(mylist))
[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd')]
回答 3
如果您使用的是长列表,则列表理解似乎会更快,更不用说可读性了。
~$ python -mtimeit -s"mylist = ['a','b','c','d']" "list(enumerate(mylist))"
1000000 loops, best of 3: 1.61 usec per loop
~$ python -mtimeit -s"mylist = ['a','b','c','d']" "[(i, j) for i, j in enumerate(mylist)]"
1000000 loops, best of 3: 0.978 usec per loop
~$ python -mtimeit -s"mylist = ['a','b','c','d']" "[t for t in enumerate(mylist)]"
1000000 loops, best of 3: 0.767 usec per loop
If you’re using long lists, it appears the list comprehension’s faster, not to mention more readable.
~$ python -mtimeit -s"mylist = ['a','b','c','d']" "list(enumerate(mylist))"
1000000 loops, best of 3: 1.61 usec per loop
~$ python -mtimeit -s"mylist = ['a','b','c','d']" "[(i, j) for i, j in enumerate(mylist)]"
1000000 loops, best of 3: 0.978 usec per loop
~$ python -mtimeit -s"mylist = ['a','b','c','d']" "[t for t in enumerate(mylist)]"
1000000 loops, best of 3: 0.767 usec per loop
回答 4
这是一种实现方法:
>>> mylist = ['a', 'b', 'c', 'd']
>>> [item for item in enumerate(mylist)]
[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd')]
或者,您可以执行以下操作:
>>> [(i, j) for i, j in enumerate(mylist)]
[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd')]
出现错误的原因是您错过了()i
并j
使其成为一个元组。
Here’s a way to do it:
>>> mylist = ['a', 'b', 'c', 'd']
>>> [item for item in enumerate(mylist)]
[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd')]
Alternatively, you can do:
>>> [(i, j) for i, j in enumerate(mylist)]
[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd')]
The reason you got an error was that you were missing the () around i
and j
to make it a tuple.
回答 5
明确说明元组。
[(i, j) for (i, j) in enumerate(mylist)]
Be explicit about the tuples.
[(i, j) for (i, j) in enumerate(mylist)]
回答 6
所有人都很好回答。我知道这里的问题是枚举所特有的,但是这样的话,又是另一个角度
from itertools import izip, count
a = ["5", "6", "1", "2"]
tupleList = list( izip( count(), a ) )
print(tupleList)
如果必须在性能方面并行迭代多个列表,它将变得更加强大。只是一个想法
a = ["5", "6", "1", "2"]
b = ["a", "b", "c", "d"]
tupleList = list( izip( count(), a, b ) )
print(tupleList)
All great answer guys. I know the question here is specific to enumeration but how about something like this, just another perspective
from itertools import izip, count
a = ["5", "6", "1", "2"]
tupleList = list( izip( count(), a ) )
print(tupleList)
It becomes more powerful, if one has to iterate multiple lists in parallel in terms of performance. Just a thought
a = ["5", "6", "1", "2"]
b = ["a", "b", "c", "d"]
tupleList = list( izip( count(), a, b ) )
print(tupleList)
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。