Python For循环仅从列表中获取奇数索引

Python For循环仅从列表中获取奇数索引

free_spacelunn = 900
disksize = [100,60,89,50,40,10,90,89,76]

print(disksize)

if (len(disksize) > 0):

    for valy in disksize:



        print(valy)
        if(valy < free_spacelunn):

            free_spacelunn = free_spacelunn - valy
            disksize.remove(valy)
            print(free_spacelunn)
print(disksize)
--------------
'''
For loop is only taking odd index value not sure why, there are two print statment for debug. Sample out put is tagged below.
[100, 60, 89, 50, 40, 10, 90, 89, 76]
100
800
89
711
40
671
90
581
76
505
[60, 50, 10, 89]

答案1

disksize从迭代的列表中删除了元素for。不要这样做。

相关内容