Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
一个python程序,用于从列表中保留第一个元素,并删除下一个元素_Python - Fatal编程技术网

一个python程序,用于从列表中保留第一个元素,并删除下一个元素

一个python程序,用于从列表中保留第一个元素,并删除下一个元素,python,Python,我正在尝试制作一个python程序,它保留列表中的第一个元素,并删除下一个元素 因此,如果您有以下列表: list1=["This","is","test","only"] 程序将输出: ["This","test] 注: •用户将填写列表,因此我们不知道其长度。使用step作为2在: 步长仅为2的A是您在此处想要的: list1=["This","is

我正在尝试制作一个python程序,它保留列表中的第一个元素,并删除下一个元素

因此,如果您有以下列表:

list1=["This","is","test","only"]
程序将输出:

["This","test]
注:


•用户将填写列表,因此我们不知道其长度。

使用
step
作为
2
在:

步长仅为
2
的A是您在此处想要的:

list1=["This","is","test","only"]
list2 = list1[::2]
print(list2)
结果:

['This', 'test']

根据示例,“保留列表中的第一个元素,删除下一个”并不是一个很好的描述。你想保留所有其他元素吗?@BadisKerdellou请别忘了对答案进行投票。
['This', 'test']