Python for loops only every second Item

…衆ロ難τιáo~ 提交于 2020-05-14 14:47:10

问题


never had such a problem and do not know how to solve or even call it...

types = [105000, 105001, 105002, 105003, 105004, 105005, 105006, 105007]
for type in types:
    print type

prints only

105000
105002
105004
105006

but I don't know why.

using a function to generate an array from input like

self.arrSplit(105000,105001,105002,105003,105004,105005,105006,105007)

function:

    def arrSplit(self, arr):
        itms = []
        for it in arr.split(','):
            tt = it.split('-')
            if (len(tt) >= 2 and int(tt[0]) < int(tt[1])):
                for i in range(int(tt[0]), int(tt[1])+1):
                    itms.append(i)
            else:
                itms.append(int(tt[0]))
        return itms

to complete this the code looks like this (its the minimum)

types = self.arrSplit(105000,105001,105002,105003,105004,105005,105006,105007)
print types
for type in types:
print type

prints:

[105000, 105001, 105002, 105003, 105004, 105005, 105006, 105007]
105000
105002
105004
105006

回答1:


For the for loop you should use this:

types = [105000, 105001, 105002, 105003, 105004, 105005, 105006, 105007]
for numbers in types:
    print numbers

This worked for me, but I can't explain why it only printed evens.



来源:https://stackoverflow.com/questions/35813004/python-for-loops-only-every-second-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!