英文:
Subsetting one lost based on values of other list
问题
根据第二个列表的值,提取第一个列表的值,可以更简洁地编写如下:
List1[4:7]
这样可以避免多次使用 List2
的名称。
英文:
Let say I have below two lists
List1 = [3,4,5,6,8,3,2,4,6,7,8,6]
List2 = [4, 7]
Now, based on the values of the second list, I want to extract the values of the first list. So trivially I would write
List1[List2[0] : List2[1]]
While it is fine, I want to make above line more concise. As you see I had to use the name List2
twice in above line. But to maintain my overall writing style, I want to call List2
only once as minimum number possible (perhaps)
Is there really any way to achieve this?
答案1
得分: 0
print(List1[slice(*List2)])
输出:
[8, 3, 2]
英文:
Try:
print(List1[slice(*List2)])
Prints:
[8, 3, 2]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论