Explicitly select items from a list or tuple

前端 未结 8 1794

I have the following Python list (can also be a tuple):

myList = [\'foo\', \'bar\', \'baz\', \'quux\']

I can say

>>&g         


        
相关标签:
8条回答
  • 2020-11-27 10:59
    >>> map(myList.__getitem__, (2,2,1,3))
    ('baz', 'baz', 'bar', 'quux')
    

    You can also create your own List class which supports tuples as arguments to __getitem__ if you want to be able to do myList[(2,2,1,3)].

    0 讨论(0)
  • 2020-11-27 11:01
    list( myBigList[i] for i in [87, 342, 217, 998, 500] )
    

    I compared the answers with python 2.5.2:

    • 19.7 usec: [ myBigList[i] for i in [87, 342, 217, 998, 500] ]

    • 20.6 usec: map(myBigList.__getitem__, (87, 342, 217, 998, 500))

    • 22.7 usec: itemgetter(87, 342, 217, 998, 500)(myBigList)

    • 24.6 usec: list( myBigList[i] for i in [87, 342, 217, 998, 500] )

    Note that in Python 3, the 1st was changed to be the same as the 4th.


    Another option would be to start out with a numpy.array which allows indexing via a list or a numpy.array:

    >>> import numpy
    >>> myBigList = numpy.array(range(1000))
    >>> myBigList[(87, 342, 217, 998, 500)]
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    IndexError: invalid index
    >>> myBigList[[87, 342, 217, 998, 500]]
    array([ 87, 342, 217, 998, 500])
    >>> myBigList[numpy.array([87, 342, 217, 998, 500])]
    array([ 87, 342, 217, 998, 500])
    

    The tuple doesn't work the same way as those are slices.

    0 讨论(0)
  • 2020-11-27 11:05

    What about this:

    from operator import itemgetter
    itemgetter(0,2,3)(myList)
    ('foo', 'baz', 'quux')
    
    0 讨论(0)
  • 2020-11-27 11:05

    Maybe a list comprehension is in order:

    L = ['a', 'b', 'c', 'd', 'e', 'f']
    print [ L[index] for index in [1,3,5] ]
    

    Produces:

    ['b', 'd', 'f']
    

    Is that what you are looking for?

    0 讨论(0)
  • 2020-11-27 11:15

    It isn't built-in, but you can make a subclass of list that takes tuples as "indexes" if you'd like:

    class MyList(list):
    
        def __getitem__(self, index):
            if isinstance(index, tuple):
                return [self[i] for i in index]
            return super(MyList, self).__getitem__(index)
    
    
    seq = MyList("foo bar baaz quux mumble".split())
    print seq[0]
    print seq[2,4]
    print seq[1::2]
    

    printing

    foo
    ['baaz', 'mumble']
    ['bar', 'quux']
    
    0 讨论(0)
  • I just want to point out, even syntax of itemgetter looks really neat, but it's kinda slow when perform on large list.

    import timeit
    from operator import itemgetter
    start=timeit.default_timer()
    for i in range(1000000):
        itemgetter(0,2,3)(myList)
    print ("Itemgetter took ", (timeit.default_timer()-start))
    

    Itemgetter took 1.065209062149279

    start=timeit.default_timer()
    for i in range(1000000):
        myList[0],myList[2],myList[3]
    print ("Multiple slice took ", (timeit.default_timer()-start))
    

    Multiple slice took 0.6225321444745759

    0 讨论(0)
提交回复
热议问题