简体   繁体   English

python for循环,如果不能正常工作

[英]python for loop and if not working properly

I have below code and it seems 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)] 

The col should be col 应该是

GA02220_04,GA02230_04,GA02240_04,GA02250_04,GA02260_04,GA02270_04,GA02280_04,GA02290_04,GA02291_04

but some how I only get但有些我只是得到

GA02220_04,GA02230_04,GA02240_04,GA02250_04,GA02260_04,GA02270_04,GA02280_04,GA02290_04

Can someone please tell me what I not getting GA02291_04?有人可以告诉我我没有得到 GA02291_04 吗?

you defined cylinderCount as a list, and then try to compare with a number, try changing.您将cylinderCount定义为列表,然后尝试与数字进行比较,尝试更改。

...
if cylinderCount == 8:
....

to

...
if cylinderCount[no] == 8:
...

Your code example is big hard to digest, since you assign col but don't use it.您的代码示例很难消化,因为您分配了col但不使用它。 This does the exact same thing as your code, but then cleaned-up and more DRY.这与您的代码完全相同,但随后进行了清理并且更加干燥。 Small assumption since your indentation is a bit off.小假设,因为您的缩进有点偏离。

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)

Printing instead of append the col result in this:打印而不是附加 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']

You compare cylinderCount with 8 .您将cylinderCount8进行比较。 Since cylinderCount is defined as [8,5,5,8] this will never be true.由于cylinderCount被定义为[8,5,5,8]这永远不会是真的。 Maybe you meant cylinderCount[no] .也许你的意思是cylinderCount[no] In this case the program is overcomplicated and could be written as :在这种情况下,程序过于复杂,可以写成:

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)

With the result:结果:

['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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM