creating a new list with subset of list using index in python

后端 未结 3 1388
小鲜肉
小鲜肉 2021-01-07 17:11

A list:

a = [\'a\', \'b\', \'c\', 3, 4, \'d\', 6, 7, 8]

I want a list using a subset of a using a[0:2],a[4], a[6:],

t

3条回答
  •  隐瞒了意图╮
    2021-01-07 17:41

    Suppose

    a = ['a', 'b', 'c', 3, 4, 'd', 6, 7, 8]
    

    and the list of indexes is stored in

    b= [0, 1, 2, 4, 6, 7, 8]
    

    then a simple one-line solution will be

    c = [a[i] for i in b]
    

提交回复
热议问题