Skip to main content
 首页 » 编程设计

python之如何在python中选择特定索引的元素

2025年05月04日14think

说,

A = [1,2,3,4] 

我想得到的是 A 的特定元素,例如只有第一和第三个元素。

所以输出应该是这样的:

[1, 3] 

我该怎么做?

print(A(0,2)) 不是我知道的正确命令,因此寻找正确的命令。

请您参考如下方法:

你可以尝试使用slice notation .

print(A[::2]) # this will print each second element of the list.