Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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
for循环卡在中间(Python)_Python_Loops - Fatal编程技术网

for循环卡在中间(Python)

for循环卡在中间(Python),python,loops,Python,Loops,我试图通过每次弹出一个元素来弹出少于四个的所有元素。然而,我不知道为什么我的循环在我的列表中间停止工作,留下了一堆没有弹出的1 nums =[16, 95, 100, 3, 12, 5, 8, 5, 1, 3, 1, 12, 4, 1, 4, 5, 4, 1, 1, 1, 1, 2, 1, 1, 3, 1, 1, 1, 2, 1, 1, 1, 1, 4, 1, 4, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1, 2,

我试图通过每次弹出一个元素来弹出少于四个的所有元素。然而,我不知道为什么我的循环在我的列表中间停止工作,留下了一堆没有弹出的1

nums =[16, 95, 100, 3, 12, 5, 8, 5, 1, 3, 1, 12, 4, 1, 4, 5, 4, 1, 1, 1, 1, 2, 1, 1, 3, 1, 1, 1, 2, 1, 1, 1, 1, 4, 1, 4, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1, 2, 1, 1, 2, 1, 2, 1]

for checker in nums:
    if checker < 4:
        nums.pop(nums.index(checker))

print(nums) #[16, 95, 100, 12, 5, 8, 5, 3, 12, 4, 4, 5, 4, 3, 4, 4, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1, 2, 1, 1, 2, 1, 2, 1]
nums=[16,95,100,3,12,5,8,5,1,3,1,12,4,1,4,5,4,1,1,1,1,2,1,1,1,3,1,1,1,1,2,1,1,1,1,4,1,2,1,1,1,1,1,1,1,1,1,1,1,1,2,1]
对于nums中的检查器:
如果检查者<4:
nums.pop(nums.index(checker))
打印(nums)#[16,95,100,12,5,8,5,3,12,4,4,5,4,3,4,4,4,1,1,1,1,2,1,1,1,1,1,1,1,2,1,1,1,2,1]

这是因为您正在更改for循环中列表的大小。

请看,您可以使用列表理解:
nums=[x代表x,如果x>=4]
请告诉我如何解决它!