Explicitly select items from a list or tuple

前端 未结 8 1942

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)].

提交回复
热议问题