Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.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_List - Fatal编程技术网

Python 从列表中每隔三个项目删除一次(在列表中定期删除项目)

Python 从列表中每隔三个项目删除一次(在列表中定期删除项目),python,list,Python,List,我想从列表中删除第三项。 例如: list1 = list(['a','b','c','d','e','f','g','h','i','j']) 删除三个索引的倍数后,列表将为: ['a','b','d','e','g','h','j'] 我怎样才能做到这一点 [v for i, v in enumerate(list1) if (i + 1) % 3 != 0] 似乎您希望列表中的第三项(实际上位于索引2)消失。这就是+1的作用。您可以使用枚举(): 或者,您可以创建列表的副本,并每隔一

我想从列表中删除第三项。 例如:

list1 = list(['a','b','c','d','e','f','g','h','i','j'])
删除三个索引的倍数后,列表将为:

['a','b','d','e','g','h','j']
我怎样才能做到这一点

[v for i, v in enumerate(list1) if (i + 1) % 3 != 0]

似乎您希望列表中的第三项(实际上位于索引2)消失。这就是
+1
的作用。

您可以使用
枚举()

或者,您可以创建列表的副本,并每隔一段时间删除这些值。例如:

>>> y = list(x) # where x is the list mentioned in above example
>>> del y[2::3] # y[2::3] = ['c', 'f', 'i']
>>> y
['a', 'b', 'd', 'e', 'g', 'h', 'j']

谢谢我知道这些事情很简单,但我在两年后再次尝试python。它的基本列表理解和索引,我不得不说我太生疏了。这就是为什么
StackOverflow
社区正在为人们服务。我很高兴能帮助你。:)@Moinuddinqadri是一种以相等间隔删除项目的好方法。谢谢你
>>> y = list(x) # where x is the list mentioned in above example
>>> del y[2::3] # y[2::3] = ['c', 'f', 'i']
>>> y
['a', 'b', 'd', 'e', 'g', 'h', 'j']