我想要做的是从列表中引用几个不同的范围,即我想要4-6个元素,12-18个元素等.这是我最初的尝试:
test = theList[4:7, 12:18]
Run Code Online (Sandbox Code Playgroud)
我希望给予的做法与:
test = theList[4,5,6,12,13,14,15,16,17]
Run Code Online (Sandbox Code Playgroud)
但是我遇到了语法错误.最好/最简单的方法是什么?
您可以添加两个列表.
>>> theList = list(range(20))
>>> theList[4:7] + theList[12:18]
[4, 5, 6, 12, 13, 14, 15, 16, 17]
Run Code Online (Sandbox Code Playgroud)