简体   繁体   English

Python数据结构:python中两种类型的for循环之间的区别?

[英]Python Data Structure: Difference between two types of for-loop in python?

I was thinking this question should be asked on SO, but I was not able to find it somehow(Let me know in the comment section if there was one, i will delete this post) 我当时以为应该问这个问题,但是我还是找不到(请在评论部分告诉我,如果有的话,我会删除这篇文章)

It has came to my attention that when we do list replacement, it only works if we are loop through the list by index. 引起我注意的是,当我们进行列表替换时,仅当我们按索引循环遍历列表时,它才起作用。 Why? 为什么?

myList = ['a','b','c','d','e']
for item in myList:
    if item == 'a':
        item = 's'
print("First loop:",myList)           //It prints ['a','b','c','d','e']


for i in range(len(myList)):
    if myList[i] == 'a':
        myList[i] = 's'
print("Second loop:",myList)          //It prints ['s','b','c','d','e']

I have tried to read the python control flow documentation: https://docs.python.org/3/tutorial/controlflow.html but it does not really answer my question. 我已经尝试阅读python控制流文档: https : //docs.python.org/3/tutorial/controlflow.html,但它并不能真正回答我的问题。

In the first loop, the line item = 's' only changes the value of the locale variable item inside the loop, which is updated in each iterations by the next value in the list. 在第一个循环中,line item = 's'仅更改循环内的语言环境变量item的值,该值在每次迭代中由列表中的下一个值更新。 It is not a reference to the list itself. 它不是对列表本身的引用。

In your first for loop, "item" is just a variable that gets assigned to whichever list item the loop has got to. 在您的第一个for循环中,“ item”只是一个变量,该变量被分配给循环必须到达的任何列表项。 Reassigning the variable doesn't affect the list. 重新分配变量不会影响列表。 In the second loop, you directly change the list item, which is why it shows up when you print the list. 在第二个循环中,您直接更改列表项,这就是为什么在打印列表时显示它。

In each iteration of the first loop, the variable item gets assigned to each item in the list. 在第一个循环的每次迭代中,变量item都分配给列表中的每个项。 When the if condition is satisfied, you then only reassign the variable item to 's' , but that does not change the content of the list. 如果满足if条件,则仅将变量item重新分配为's' ,但这不会更改列表的内容。

In the second loop, you are re-assigning the contents of my_list , as you are assigning the ith item to 's' with the line. 在第二个循环中,您将重新分配my_list的内容,因为您将第i个项目分配给该行的's'

    myList[i] = 's'

Consider also a simpler example: 还考虑一个更简单的示例:

    myList = ['a', 'b', 'c']
    item = myList[0]  # assign item to 'a'
    item = 's'  # re-assign item variable to 's', does not change list
    myList[0] = 's'  # change the content of the first item in the list

Also have a look at this: Python : When is a variable passed by reference and when by value? 还可以看一下: Python:何时通过引用传递变量,何时按值传递变量?

For an example of why the first loop doesn't work, check this: 有关为什么第一个循环不起作用的示例,请检查以下内容:

myList = ['a','b','c','d','e']
item = myList[0]
item = 's'

# this is an example equivalent to what the first loop does
# the values in myList remain unchanged

And an example equivalent to the second loop: 还有一个等效于第二个循环的示例:

myList = ['a','b','c','d','e']
myList[0] = 's'

# the first value in myList is changed to 's'

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

相关问题 2 个不同的 for-loop(python) 之间有什么区别: - what is the difference between the 2 different for-loop(python) : Python 3中for循环的数据类型和文档 - Data types and documentation for for-loop in Python 3 Python 不正确的 for 循环缩进(Coursera Python 数据结构课程) - Python incorrect indent for for-loop (Coursera Python Data Structure courses) 这两种类型的Python指数有什么区别 - What is the difference between these two types of exponential in Python 没有双for循环的两个python矩阵之间的欧几里得距离? - Euclidian distance between two python matrixes without double for-loop? 在python for循环中获取两个单元格之间的差异 - Taking difference between two cells in python for loop 如何使用 Python 找到由不同数据类型组成的两个列表之间的差异 - How to find the difference between two lists consisted of different data types with Python 用于pos数据的python中的数据清理for循环 - Data cleaning for-loop in python for pos data python中的数据类型和数据结构有什么区别吗? - Is there any difference between data types and data structures in python? python本地数据结构“ DICTIONARY”和“ Redis”数据库有什么区别? - What is the difference between python native data structure “DICTIONARY” and “Redis” database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM