Python 创建 for 循环直到 list.length

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14532875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:44:35  来源:igfitidea点击:

Creating for loop until list.length

pythonlistfor-loop

提问by Agape

I'm reading about for loops right now, and I am curious if it's possible to do a for loop in Python like in Java.

我现在正在阅读 for 循环,我很好奇是否可以像在 Java 中那样在 Python 中执行 for 循环。

Is it even possible to do something like

甚至有可能做类似的事情

for (int i = 1; i < list.length; i++)

and can you do another for loop inside this for loop ?

你能在这个 for 循环中再做一个 for 循环吗?

Thanks

谢谢

采纳答案by Ashwini Chaudhary

In Python you can iterate over the listitself:

在 Python 中,您可以迭代list自身:

for item in my_list:
   #do something with item

or to use indices you can use xrange():

或使用索引,您可以使用xrange()

for i in xrange(1,len(my_list)):    #as indexes start at zero so you 
                                    #may have to use xrange(len(my_list))
    #do something here my_list[i]

There's another built-in function called enumerate(), which returns both item and index:

还有另一个名为 的内置函数enumerate(),它返回 item 和 index:

for index,item in enumerate(my_list):
    # do something here

examples:

例子:

In [117]: my_lis=list('foobar')

In [118]: my_lis
Out[118]: ['f', 'o', 'o', 'b', 'a', 'r']

In [119]: for item in my_lis:
    print item
   .....:     
f
o
o
b
a
r

In [120]: for i in xrange(len(my_lis)):
    print my_lis[i]
   .....:     
f
o
o
b
a
r

In [122]: for index,item in enumerate(my_lis):
    print index,'-->',item
   .....:     
0 --> f
1 --> o
2 --> o
3 --> b
4 --> a
5 --> r

回答by Vahid Farahmand

You could learn about Python loops here: http://en.wikibooks.org/wiki/Python_Programming/Loops

您可以在此处了解 Python 循环:http: //en.wikibooks.org/wiki/Python_Programming/Loops

You have to know that Python doesn't have { and } for start and end of loop, instead it depends on tab chars you enter in first of line, I mean line indents.

您必须知道 Python 没有 { 和 } 用于循环的开始和结束,而是取决于您在第一行输入的制表符,我的意思是行缩进。

So you can do loop inside loop with double tab (indent)

所以你可以用双标签(缩进)在循环内循环

An example of double loop is like this:

双循环的一个例子是这样的:

onetoten = range(1,11)
tentotwenty = range(10,21)
for count in onetoten:
    for count2 in tentotwenty
        print(count2)

回答by Felix Kling

Yes you can, with range[docs]:

是的,您可以,使用range[docs]

for i in range(1, len(l)):
    # i is an integer, you can access the list element with l[i]

but if you are accessing the list elements anyway, it's more natural to iterate over them directly:

但是,如果您无论如何都在访问列表元素,则直接遍历它们会更自然:

for element in l:
   # element refers to the element in the list, i.e. it is the same as l[i]

If you want to skip the the first element, you can slice the list [tutorial]:

如果你想跳过第一个元素,你可以切片列表[tutorial]

for element in l[1:]:
    # ...


can you do another for loop inside this for loop

你能在这个 for 循环中再做一个 for 循环吗

Sure you can.

你当然可以。

回答by RomanI

The answer depends on what do you need a loop for.

答案取决于您需要循环做什么。

of course you can have a loop similar to Java:

当然你可以有一个类似于 Java 的循环:

for i in xrange(len(my_list)):

but I never actually used loops like this,

但我从未真正使用过这样的循环,

because usually you want to iterate

因为通常你想迭代

for obj in my_list

or if you need an index as well

或者如果您还需要索引

for index, obj in enumerate(my_list)

or you want to produce another collection from a list

或者您想从列表中生成另一个集合

map(some_func, my_list)

[somefunc[x] for x in my_list]

also there are itertoolsmodule that covers most of iteration related cases

还有一些itertools模块涵盖了大多数与迭代相关的案例

also please take a look at the builtins like any, max, min, all, enumerate

也请看一下内置函数,如any, max, min, all,enumerate

I would say - do not try to write Java-like code in python. There is always a pythonic way to do it.

我会说 - 不要尝试在 python 中编写类似 Java 的代码。总有一种pythonic的方式来做到这一点。

回答by Vidul

I'd try to search for the solution by google and the string Python for statement, it is as simple as that. The firstlink says everything. (A great forum, really, but its usage seems to look sometimes like the usage of the Microsoft understanding of all their GUI products' benefits: windows inside, idiots outside.)

我会尝试通过 google 和字符串Python for statement搜索解决方案,就这么简单。第一个链接说明了一切。(一个很棒的论坛,真的,但它的用法有时看起来就像 Microsoft 对其所有 GUI 产品的好处的理解的用法:窗口在里面,白痴在外面。)