我有以下代碼,它似乎無法正常作業。
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 嗎?
uj5u.com熱心網友回復:
您定義cylinderCount為一個串列,然后嘗試與一個數字進行比較,嘗試更改。
...
if cylinderCount == 8:
....
至
...
if cylinderCount[no] == 8:
...
uj5u.com熱心網友回復:
您的代碼示例很難消化,因為您分配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']
uj5u.com熱心網友回復:
你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']
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/482604.html
上一篇:無法設定輸入欄位的值
下一篇:陣列公式整數
