[英]python for loop and if not working properly
我有以下代码,它似乎无法正常工作。
cylinderCount = [8,5,5,8]
engineCount = 4
for no in range(engineCount):
for i in range(cylinderCount[no]):
engineNo = str(no+1)
if cylinderCount == 8:
col = ["GA022"+str(i) + '_0'+str(engineNo) for i in range(20, cylinderCount[no]*10+11,10)] + ['GA02291'+'_0'+str(engineNo)]
else:
col = ["GA022"+str(i) + '_0'+str(engineNo) for i in range(20, cylinderCount[no]*10+11,10)]
col 应该是
GA02220_04,GA02230_04,GA02240_04,GA02250_04,GA02260_04,GA02270_04,GA02280_04,GA02290_04,GA02291_04
但有些我只是得到
GA02220_04,GA02230_04,GA02240_04,GA02250_04,GA02260_04,GA02270_04,GA02280_04,GA02290_04
有人可以告诉我我没有得到 GA02291_04 吗?
您将cylinderCount
定义为列表,然后尝试与数字进行比较,尝试更改。
...
if cylinderCount == 8:
....
至
...
if cylinderCount[no] == 8:
...
您的代码示例很难消化,因为您分配了col
但不使用它。 这与您的代码完全相同,但随后进行了清理并且更加干燥。 小假设,因为您的缩进有点偏离。
cylinderCount = [8, 5, 5, 8]
engineCount = 4
rows = []
for engineno_int, cylinders in enumerate(cylinderCount):
engineNo = str(engineno_int + 1)
for i in range(cylinders):
col = [f"GA022{i}_0{engineNo}" for i in range(20, cylinders * 10 + 11, 10)]
if cylinders == 8:
col += [f"GA02291_0{engineNo}"]
rows.append(col)
打印而不是附加 col 结果如下:
['GA02220_01', 'GA02230_01', 'GA02240_01', 'GA02250_01', 'GA02260_01', 'GA02270_01', 'GA02280_01', 'GA02290_01', 'GA02291_01']
['GA02220_01', 'GA02230_01', 'GA02240_01', 'GA02250_01', 'GA02260_01', 'GA02270_01', 'GA02280_01', 'GA02290_01', 'GA02291_01']
['GA02220_01', 'GA02230_01', 'GA02240_01', 'GA02250_01', 'GA02260_01', 'GA02270_01', 'GA02280_01', 'GA02290_01', 'GA02291_01']
['GA02220_01', 'GA02230_01', 'GA02240_01', 'GA02250_01', 'GA02260_01', 'GA02270_01', 'GA02280_01', 'GA02290_01', 'GA02291_01']
['GA02220_01', 'GA02230_01', 'GA02240_01', 'GA02250_01', 'GA02260_01', 'GA02270_01', 'GA02280_01', 'GA02290_01', 'GA02291_01']
['GA02220_01', 'GA02230_01', 'GA02240_01', 'GA02250_01', 'GA02260_01', 'GA02270_01', 'GA02280_01', 'GA02290_01', 'GA02291_01']
['GA02220_01', 'GA02230_01', 'GA02240_01', 'GA02250_01', 'GA02260_01', 'GA02270_01', 'GA02280_01', 'GA02290_01', 'GA02291_01']
['GA02220_01', 'GA02230_01', 'GA02240_01', 'GA02250_01', 'GA02260_01', 'GA02270_01', 'GA02280_01', 'GA02290_01', 'GA02291_01']
['GA02220_02', 'GA02230_02', 'GA02240_02', 'GA02250_02', 'GA02260_02']
['GA02220_02', 'GA02230_02', 'GA02240_02', 'GA02250_02', 'GA02260_02']
['GA02220_02', 'GA02230_02', 'GA02240_02', 'GA02250_02', 'GA02260_02']
['GA02220_02', 'GA02230_02', 'GA02240_02', 'GA02250_02', 'GA02260_02']
['GA02220_02', 'GA02230_02', 'GA02240_02', 'GA02250_02', 'GA02260_02']
['GA02220_03', 'GA02230_03', 'GA02240_03', 'GA02250_03', 'GA02260_03']
['GA02220_03', 'GA02230_03', 'GA02240_03', 'GA02250_03', 'GA02260_03']
['GA02220_03', 'GA02230_03', 'GA02240_03', 'GA02250_03', 'GA02260_03']
['GA02220_03', 'GA02230_03', 'GA02240_03', 'GA02250_03', 'GA02260_03']
['GA02220_03', 'GA02230_03', 'GA02240_03', 'GA02250_03', 'GA02260_03']
['GA02220_04', 'GA02230_04', 'GA02240_04', 'GA02250_04', 'GA02260_04', 'GA02270_04', 'GA02280_04', 'GA02290_04', 'GA02291_04']
['GA02220_04', 'GA02230_04', 'GA02240_04', 'GA02250_04', 'GA02260_04', 'GA02270_04', 'GA02280_04', 'GA02290_04', 'GA02291_04']
['GA02220_04', 'GA02230_04', 'GA02240_04', 'GA02250_04', 'GA02260_04', 'GA02270_04', 'GA02280_04', 'GA02290_04', 'GA02291_04']
['GA02220_04', 'GA02230_04', 'GA02240_04', 'GA02250_04', 'GA02260_04', 'GA02270_04', 'GA02280_04', 'GA02290_04', 'GA02291_04']
['GA02220_04', 'GA02230_04', 'GA02240_04', 'GA02250_04', 'GA02260_04', 'GA02270_04', 'GA02280_04', 'GA02290_04', 'GA02291_04']
['GA02220_04', 'GA02230_04', 'GA02240_04', 'GA02250_04', 'GA02260_04', 'GA02270_04', 'GA02280_04', 'GA02290_04', 'GA02291_04']
['GA02220_04', 'GA02230_04', 'GA02240_04', 'GA02250_04', 'GA02260_04', 'GA02270_04', 'GA02280_04', 'GA02290_04', 'GA02291_04']
['GA02220_04', 'GA02230_04', 'GA02240_04', 'GA02250_04', 'GA02260_04', 'GA02270_04', 'GA02280_04', 'GA02290_04', 'GA02291_04']
您将cylinderCount
与8
进行比较。 由于cylinderCount
被定义为[8,5,5,8]
这永远不会是真的。 也许你的意思是cylinderCount[no]
。 在这种情况下,程序过于复杂,可以写成:
cylinders = [8, 5, 5, 8]
for no, cylinder in enumerate(cylinders, start=1):
col = [f'GA022{i}_0{no}' for i in range(20, cylinder * 10 + 11, 10)]
if cylinder == 8:
col += [f'GA02291_0{no}']
print(col)
结果:
['GA02220_01', 'GA02230_01', 'GA02240_01', 'GA02250_01', 'GA02260_01', 'GA02270_01', 'GA02280_01', 'GA02290_01', 'GA02291_01']
['GA02220_02', 'GA02230_02', 'GA02240_02', 'GA02250_02', 'GA02260_02']
['GA02220_03', 'GA02230_03', 'GA02240_03', 'GA02250_03', 'GA02260_03']
['GA02220_04', 'GA02230_04', 'GA02240_04', 'GA02250_04', 'GA02260_04', 'GA02270_04', 'GA02280_04', 'GA02290_04', 'GA02291_04']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.