[英]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.