Python using enumerate inside list comprehension

后端 未结 7 2252
滥情空心
滥情空心 2020-11-28 01:49

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\

相关标签:
7条回答
  • 2020-11-28 01:49

    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)
    
    0 讨论(0)
  • 2020-11-28 02:01

    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.

    0 讨论(0)
  • 2020-11-28 02:03

    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')]
    
    0 讨论(0)
  • 2020-11-28 02:05

    Be explicit about the tuples.

    [(i, j) for (i, j) in enumerate(mylist)]
    
    0 讨论(0)
  • 2020-11-28 02:07

    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')]
    
    0 讨论(0)
  • 2020-11-28 02:08

    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
    
    0 讨论(0)
提交回复
热议问题