Python gets the list according to the index of a list

If you can get the index of a certain heap of a certain list, store it in the list

The list does not support such queries:

ls=[1,2,3,4,5,6,7,8,9,0]
ls[2,3,6]
##或者
ls=[1,2,3,4,5,6,7,8,9,0]
ls[[2,3,6]]

Will report an error:

TypeError: list indices must be integers or slices, not tuple

 or

TypeError: list indices must be integers or slices, not list

The correct way is actually very simple:

ls=[1,2,3,4,5,6,7,8,9,0]#list
index=[2,3,6]#index list 
[ls[i]for i in index]

operation result:

[3, 4, 7]

 

Guess you like

Origin blog.csdn.net/zhou_438/article/details/109308777