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
要获得与索引一起打印的值,我可以使用Python的枚举函数,像这样。
>>> 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?
那么,我的问题是:在列表理解中使用枚举的正确方法是什么?
7 个解决方案
#1
131
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:
你需要把i、j放在一个元组中,以使列表理解工作。或者,如果枚举()已经返回一个元组,那么您可以直接返回它,而不先将其解压缩:
[pair for pair in enumerate(mylist)]
Either way, the result that gets returned is as expected:
无论哪种方式,返回的结果都是预期的:
> [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd')]
#2
39
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:
这是不一致的(IMHO),混淆了字典的理解语法:
>>> {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')]
#3
30
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')]
#4
11
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
#5
8
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.
你犯错误的原因是你错过了i和j,使它成为一个元组。
#6
5
Be explicit about the tuples.
对元组要明确。
[(i, j) for (i, j) in enumerate(mylist)]
#7
0
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)
#1
131
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:
你需要把i、j放在一个元组中,以使列表理解工作。或者,如果枚举()已经返回一个元组,那么您可以直接返回它,而不先将其解压缩:
[pair for pair in enumerate(mylist)]
Either way, the result that gets returned is as expected:
无论哪种方式,返回的结果都是预期的:
> [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd')]
#2
39
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:
这是不一致的(IMHO),混淆了字典的理解语法:
>>> {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')]
#3
30
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')]
#4
11
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
#5
8
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.
你犯错误的原因是你错过了i和j,使它成为一个元组。
#6
5
Be explicit about the tuples.
对元组要明确。
[(i, j) for (i, j) in enumerate(mylist)]
#7
0
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)