Slicing a list using a variable, in Python

前端 未结 5 2041
攒了一身酷
攒了一身酷 2020-12-05 22:36

Given a list

a = range(10)

You can slice it using statements such as

a[1]
a[2:4]

However, I want to do th

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-05 23:27

    Why does it have to be a single variable? Just use two variables:

    i, j = 2, 4
    a[i:j]
    

    If it really needs to be a single variable you could use a tuple.

提交回复
热议问题