Python:for 循环 - for i in range(0,len(list) vs for i in list

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32930246/
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-19 12:29:37  来源:igfitidea点击:

Python: for loops - for i in range(0,len(list) vs for i in list

pythonalgorithmsorting

提问by Intrepid Diamond

This is a really simple python mechanics question. Why can't I just say for i in range original_list instead of for i in range(0, len(original_list)). Do people usually use range over the former? Thanks!

这是一个非常简单的 Python 力学问题。为什么我不能只说 for i in range original_list 而不是 for i in range(0, len(original_list))。人们通常使用范围而不是前者吗?谢谢!

# "If I give you an array with negative and positive numbers (i.e. {3,2,-3,6,4,-7}) and asked you to sort it so that the negative numbers appeared first but you didn't change the relative order of the remaining numbers, how would you do it? (i.e. the final result would be {-3,-7,3,2,6,4}).

original_list = [3, 2, -3, 6, 4, -7]
pos_list = []
neg_list = []

for i in range(0, len(original_list)):
    if original_list[i] < 0:
        neg_list.append(original_list[i])
    else:
        pos_list.append(original_list[i])

print neg_list + pos_list

采纳答案by ozgur

In your case, since you don't need to use the index of the items in the list, you can just iterate over it using for in:

在您的情况下,由于您不需要使用列表中项目的索引,您可以使用for in以下方法对其进行迭代:

>>> for item in original_list:
...     if item < 0:
...         ...

If you want to iterate over the indexesof items in your list, use for in range(..):

如果要遍历列表中项目的索引,请使用for in range(..)

>>> for i in range(len(original_list)):
...     if original_list[i] < 0:
...         ...

Alternatively, you might also want to use enumerate()if you need both item and index in loop's body:

或者,enumerate()如果您需要循环体中的 item 和 index ,您可能还想使用:

>>> for i, item in enumerate(original_list):
...    if item < 0:
...        ...

By this way, you also eliminate the use of original_list[i].

通过这种方式,您还可以避免使用original_list[i].

回答by idjaw

If you open up your interpreter and do the following:

如果您打开解释器并执行以下操作:

help(range)

help(range)

You will get this:

你会得到这个:

Help on built-in function range in module __builtin__:

range(...)
    range(stop) -> list of integers
    range(start, stop[, step]) -> list of integers

    Return a list containing an arithmetic progression of integers.
    range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.
    When step is given, it specifies the increment (or decrement).
    For example, range(4) returns [0, 1, 2, 3].  The end point is omitted!
    These are exactly the valid indices for a list of 4 elements.

As you can see from the description, it is the way the range works. It takes a starting position, and then a stopping position, and then an optional step to go up by x each time.

从描述中可以看出,这是范围的工作方式。它需要一个起始位置,然后是一个停止位置,然后是每次上升 x 的可选步骤。

edit

编辑

To answer based off of your comment, the reason why you are getting an index out of range, is because when you do this:

根据您的评论回答,您获得索引超出范围的原因是因为当您这样做时:

for i in arr:
    print(arr[i])

That won't work, because for i in arrwill give you the valuesof each item in the array. So, what you are doing then is actually putting in a value in to arr as if it is the index. But it is not.

这是行不通的,因为for i in arr会给你数组中每个项目的。所以,你正在做的实际上是在 arr 中放入一个值,就好像它是索引一样。但事实并非如此。

When you do this:

当你这样做时:

for i in range(0, len(arr))

for i in range(0, len(arr))

What is happening here is you are getting an incremental value that is based on the size of the array, so that way you can use that value as an index to your list.

这里发生的事情是您获得了一个基于数组大小的增量值,这样您就可以使用该值作为列表的索引。

回答by Jared Mackey

Don't use range to iterate over a list of values. Do for item in list. If you need the index use enumerate.

不要使用 range 迭代值列表。做for item in list。如果您需要索引使用枚举。

for index, value in enumerate(original_list):
    print index
    print value

Or

或者

for item in original_list:
    if item < 0:
        neg_list.append(item)
    else:
        pos_list.append(item)

You were probably getting the exception using for item in original_list:because you were trying to do neg_list.append(original_list[item])which would could lead to something like original_list[-3]or original_list[6]which would be out of range.

您可能在使用时遇到异常,for item in original_list:因为您正在尝试执行neg_list.append(original_list[item])可能导致类似original_list[-3]original_list[6]超出范围的事情。

回答by lvc

The construction range(len(my_sequence))is usually not considered idiomatic Python. It focuses your code on lower level mechanics than what we usually try to write, and this makes it harder to read. Because of this, using rangehere is mostly seen as a holder from people used to coding in lower level languages like C.

该构造range(len(my_sequence))通常不被认为是惯用的 Python。它将您的代码集中在比我们通常尝试编写的更低级别的机制上,这使得它更难阅读。正因为如此,range这里的使用主要被视为习惯于使用 C 等低级语言进行编码的人的持有者。

See, for example, Raymond Hettinger's talk Transforming Code into Beautiful Idiomatic Python- one of the first things he recommends is exactly changing for i in range(len(sequence))into for item in sequencewhere ever it appears; he then goes on to mention enumerateand zipto cover situations where you might otherwise be tempted to revert to using range. He also mentions that the idiomatic way is faster. Aside from Python builtin types being (unsurprisingly) optimised to run idiomatic constructs quickly, it isn't hard to see why this could be true of certain other data structures - for example, a linked list can have much faster sequential than random access, meaning that loops relying on my_linked_list[i]could become a quadratic time operation rather than a linear one.

见,例如,雷蒙德赫廷杰的谈话转化代码为美丽地道的Python-他建议正好改变的第一件事for i in range(len(sequence))for item in sequence它出现的地方,直到永远。然后,他继续提到enumeratezip涵盖了您可能想恢复使用range. 他还提到惯用的方式更快。除了 Python 内置类型(不出所料)被优化以快速运行惯用构造之外,不难看出为什么这可能适用于某些其他数据结构 - 例如,链表的顺序访问速度比随机访问快得多,这意味着依赖的循环my_linked_list[i]可能成为二次时间运算而不是线性运算。

You can see similar advice from time to time if you follow the [python] tag over at codereview.SE.

如果您关注codereview.SE 上的 [python] 标签,您会不时看到类似的建议。

回答by Ahsanul Haque

lst=[1,2,3,4]

for i in range(lst):
    print lst[i]

You can't do that. Because:

你不能那样做。因为:

TypeError: range() integer end argument expected, got list.

类型错误:预期范围()整数结束参数,得到列表。

What you can do is:

你可以做的是:

 for i in range(len(lst)): #i is the index here
     print lst[i]

Output: 1 2 3 4

输出:1 2 3 4

Or,

或者,

 for i in lst: #i is the value here
     print i

Output:

输出:

1
2
3
4

Actually, rangeis not idiomatic python. You can avoid it. If you need index, you can use enumeratelike below:

其实,rangepython不是惯用的。你可以避免它。如果你需要索引,你可以使用enumerate如下:

 for i,j in enumerate(lst): #i is the index and j is the value here
 print i,j

Hope it is clear to you now.

希望你现在清楚了。

回答by Peter Wood

There is a recipe for partitioning in the itertoolsdocumentation:

itertools文档中有一个分区方法:

def partition(pred, iterable):
    'Use a predicate to partition entries into false entries and true entries'
    # partition(is_odd, range(10)) --> 0 2 4 6 8   and  1 3 5 7 9
    t1, t2 = tee(iterable)
    return filterfalse(pred, t1), filter(pred, t2)

You can use it like:

你可以像这样使用它:

positives, negatives = partition(lambda value: value < 0, originals)

回答by Ashen One

I don't really know why no one writes about list elements appropriation. When you use simple for element in listand you tried to write some info in the list element - that not works. Only when you use the index of element, like list[0]="Test value"you can rewrite it.

我真的不知道为什么没有人写关于列表元素挪用的文章。当您使用 simplefor element in list并尝试在列表元素中写入一些信息时 - 这不起作用。只有当你使用元素的索引时,就像 list[0]="Test value"你可以重写它。