简体   繁体   English

对于循环遍历列表

[英]For loop iterating through the list

I am learning the python from the Codeacademy website and I came across the loops section which is a little vague and hard to me. 我正在从Codeacademy网站上学习python,并且遇到了“循环”部分,这对我来说有点模糊和困难。 When the website wants to explain how does a for loop works, it gets help from lists. 当网站想要解释for循环的工作原理时,它会从列表中获得帮助。 Like so: 像这样:

for i in list34:
    #Some codes

The website says that when you run a for loop statement for a list, the for loop would iterate through the elements of the list then save them in i variable. 该网站说,当您为列表运行for loop语句时,for循环将遍历列表中的元素,然后将其保存在i变量中。

I just don't get the iterating through concept! 我只是不了解概念而已

What does it mean? 这是什么意思?

for i in list34:
    #Some codes

This snippet will go through all the items of list34 (ie, iterate through them). 该代码段将遍历 list34所有项目(即遍历它们)。 In each iteration ("step" of the loop), i will be assigned the next value from the list, so your code could do something with it (eg, print it out). 在每次迭代(循环的“步骤”)中,将从列表中为i分配下一个值,因此您的代码可以对其执行某些操作(例如,将其打印出来)。

Maybe some code example will help! 也许一些代码示例会有所帮助!

>>> li = [4,3,1,2,0]
>>> for x in li:
...     print(x)
... 
4
3
1
2
0
>>> 

What the for loop does is, it takes one item in the list at a time and assigns that item to the variable x . for循环所要做的是,它一次将列表中的一项带走,然后将该项分配给变量x As the for loop takes items of lists one by one , it is called iterating through/on the list. 因为for循环一个接一个地列出列表项,所以称为循环遍历列表。

iterating over a list, or any data structure for that matter, means that it just takes every element, one after the other, from the given structure an does something with it. 遍历一个列表或与此相关的任何数据结构,意味着它只是从给定的结构中接一个接一个地处理每个元素,并对此进行了处理。

in this case you have your i elements and you do stuff with them inside the for loop. 在这种情况下,您拥有i元素,并且可以在for循环中对其进行处理。 the for statement makes sure, that every element of the list is handled. for语句确保处理列表中的每个元素。

The for statement in Python differs a bit from what you may be used to in C or Pascal. Python中的for语句与您在C或Pascal中使用的语句有些不同。 Rather than always iterating over an arithmetic progression of numbers (like in Pascal), or giving the user the ability to define both the iteration step and halting condition (as C), Python's for statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence. Python的for语句不是遍历数字的算术级数进行迭代(例如在Pascal中),也不是让用户能够定义迭代步骤和暂停条件(如C),而是遍历任何序列项(列表或字符串),按它们在序列中出现的顺序排列。 For example (no pun intended): 例如(无双关语):

# Measure some strings:
words = ['cat', 'window', 'defenestrate']
for w in words:
    print w, len(w)

If you need to modify the sequence you are iterating over while inside the loop (for example to duplicate selected items), it is recommended that you first make a copy. 如果需要修改循环中要迭代的序列(例如,复制选定的项目),建议您首先进行复制。 Iterating over a sequence does not implicitly make a copy. 遍历序列不会隐式地创建副本。 The slice notation makes this especially convenient: 切片符号使这特别方便:

for w in words[:]:  # Loop over a slice copy of the entire list.
    if len(w) > 6:
        words.insert(0, w)

words

If you do need to iterate over a sequence of numbers, the built-in function range() comes in handy. 如果确实需要遍历一个数字序列,则内置函数range()会派上用场。 It generates lists containing arithmetic progressions: 它生成包含算术级数的列表:

range(10)

The given end point is never part of the generated list; 给定的终点永远不会成为生成列表的一部分; range(10) generates a list of 10 values, the legal indices for items of a sequence of length 10. It is possible to let the range start at another number, or to specify a different increment (even negative; sometimes this is called the 'step'): range(10)生成一个包含10个值的列表,这些索引是长度为10的序列的项目的合法索引。可以使范围从另一个数字开始,也可以指定一个不同的增量(甚至是负数;甚至是负数;有时也称为'步'):

range(5, 10)    
range(0, 10, 3)
range(-10, -100, -30)

To iterate over the indices of a sequence, you can combine range() and len() as follows: 要遍历序列的索引,可以如下组合range()和len():

a = ['Mary', 'had', 'a', 'little', 'lamb']
for i in range(len(a)):
    print i, a[i]

Read More: docs.python.org 阅读更多: docs.python.org

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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