如何根据计数器编号在循环中填充数据列表 [英] How to fill lists with data in a loop depending on counter number

查看:67
本文介绍了如何根据计数器编号在循环中填充数据列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得604个字符串(URL)并将它们插入4个列表中

每个列表应该取151个字符串(URL)



我尝试了什么:



I am trying to get 604 strings (URLs) and insert them into 4 lists
each list should take 151 string (URL)

What I have tried:

first = []
second = []
third = []
forth = []

for i in range(1, 605):
    try:
        url = "http://url/" + str(i) + ".png"

        if i <= 151:
            first.append(url)

        elif i > 151:
            if i < 303:
                second.append(url)
            else:
                continue

        elif i > 302:
            if i < 453:
                third.append(url)
            else:
                continue
        else:
            forth.append(url)
    except Exception as e:
        print("Error accrued: " + str(e))

print("first:", first)
print("second:", second)
print("third:", third)
print("forth:", forth)





我得到的结果是只有第一个和第二个列表,其余的列表没有得到字符串!!

列出第三个和四个结果为空!!

任何想法为什么这是发生



the results I get are only for the first and second list, the rest of the lists doesn't get the strings !!
list third and four results empty!!
any idea why this is happening

推荐答案

elif i > 151:
        if i < 303:
            second.append(url)
        else:
            continue





这说,如果它是< 151,然后检查它是否< 303.如果没有,继续。这就是,什么都不做。



尝试



This says, if it's < 151, then check if it's < 303. If not, continue. THat is, do nothing.

Try

elif(i> 151 && i < 303)





我假设和运算符是Python的标准



I assume the and operator is standard in Python


Quote:

我得到的结果只是第一个和第二个列表,其余的列表没有得到字符串!!

the results I get are only for the first and second list, the rest of the lists doesn't get the strings !!



使用调试器来看你的代码执行,它将帮助你理解它是如何工作的,或者是不工作的。



你的代码没有按照你期望的方式运行,或者你没有不明白为什么!



有一个几乎通用的解决方案:一步一步地在调试器上运行你的代码,检查变量。

调试器这里是为了向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它不知道您的代码应该做什么,它没有发现错误,只是通过向你展示你的目标来帮助你ñ。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]


掌握调试Visual Studio 2010 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



27.3。 pdb - Python调试器 - Python 3.6.1文档 [ ^ ]

使用Python进行调试Python征服宇宙 [ ^ ]

pdb - 交互式调试器 - 本周的Python模块 [ ^ ]



调试器只是向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。


Use the debugger to watch your code perform, it will help you to understand how it work, or don't work.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

27.3. pdb — The Python Debugger — Python 3.6.1 documentation[^]
Debugging in Python | Python Conquers The Universe[^]
pdb – Interactive Debugger - Python Module of the Week[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


这篇关于如何根据计数器编号在循环中填充数据列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆