简体   繁体   English

Python 修改和编辑列表

[英]Python modifying and editing lists

I am just starting to learn python, I was trying out some code and I wanted to ask why the two following codes function differently?我刚开始学习 python,我正在尝试一些代码,我想问为什么以下两个代码 function 不同?

First code sample:第一个代码示例:

def combine_lists(list1, list2):
  new_list = list2
  index = len(list2)
  for items in list1:
      new_list.insert(index, items)
  return(new_list)
    
Jamies_list = ["Alice", "Cindy", "Bobby", "Jan", "Peter"]
Drews_list = ["Mike", "Carol", "Greg", "Marcia"]

print(combine_lists(Jamies_list, Drews_list))

The output of this first code is correct (where I want list 1 reversed):第一个代码的 output 是正确的(我希望列表 1 颠倒):

['Mike', 'Carol', 'Greg', 'Marcia', 'Peter', 'Jan', 'Bobby', 'Cindy', 'Alice'] [“迈克”、“卡罗尔”、“格雷格”、“玛西娅”、“彼得”、“简”、“鲍比”、“辛迪”、“爱丽丝”]

Second code sample:第二个代码示例:

def combine_lists(list1, list2):
  new_list = list2
  for items in list1:
      new_list.insert(len(list2), items)
  return(new_list)
    
Jamies_list = ["Alice", "Cindy", "Bobby", "Jan", "Peter"]
Drews_list = ["Mike", "Carol", "Greg", "Marcia"]

print(combine_lists(Jamies_list, Drews_list))

But this second code outputs:但是第二个代码输出:

['Mike', 'Carol', 'Greg', 'Marcia', 'Alice', 'Cindy', 'Bobby', 'Jan', 'Peter'] [“迈克”、“卡罗尔”、“格雷格”、“玛西娅”、“爱丽丝”、“辛迪”、“鲍比”、“简”、“彼得”]

Why is there a difference in these two outputs by just assigning the len(list2) to a variable and using that for the index insertion?为什么通过将 len(list2) 分配给一个变量并将其用于索引插入,这两个输出会有所不同?

Thanks!谢谢!

This line:这一行:

    new_list = list2

Just creates a new name for the list2 list, but both variables refer to the same list.只需为list2列表创建一个新名称,但两个变量都引用同一个列表。

So, in the first example, you get the length of that list with index = len(list2) and then use the same value for index on each iteration through the for loop.因此,在第一个示例中,您使用index = len(list2)获得该列表的长度,然后通过 for 循环在每次迭代中使用相同的index值。

In the second example, you get len(list2) for each iteration, but since you're modifying new_list , you're also modifying list2 and its length will increase as you go.在第二个示例中,每次迭代都会获得len(list2) ,但由于您正在修改new_list ,因此您也在修改list2并且它的长度将随着您的 go 增加。

If you need a copy of the list, create a copy with list(new_list) = list2 or new_list = list2.copy()如果您需要列表的副本,请使用list(new_list) = list2new_list = list2.copy()创建一个副本

First code snippet第一个代码片段


def combine_lists(list1, list2):
  new_list = list2
  index = len(list2)
  for items in list1:
      new_list.insert(index, items)
      print(new_list)
  return(new_list)
    
Jamies_list = ["Alice", "Cindy", "Bobby", "Jan", "Peter"]
Drews_list = ["Mike", "Carol", "Greg", "Marcia"]

print(combine_lists(Jamies_list, Drews_list))

Output: Output:

['Mike', 'Carol', 'Greg', 'Marcia', 'Alice']
['Mike', 'Carol', 'Greg', 'Marcia', 'Cindy', 'Alice']
['Mike', 'Carol', 'Greg', 'Marcia', 'Bobby', 'Cindy', 'Alice']
['Mike', 'Carol', 'Greg', 'Marcia', 'Jan', 'Bobby', 'Cindy', 'Alice']
['Mike', 'Carol', 'Greg', 'Marcia', 'Peter', 'Jan', 'Bobby', 'Cindy', 'Alice']
['Mike', 'Carol', 'Greg', 'Marcia', 'Peter', 'Jan', 'Bobby', 'Cindy', 'Alice']

For your first code, you're inserting new_list.insert(index, items) every new item on the same position for a certain period ( len(list2) ) .对于您的第一个代码,您将new_list.insert(index, items)插入同一 position 上的每个新项目一段时间( len(list2) ) Every time when you insert it get the same index and last insert element get move to the index+1 position and that's why it seems Jamies_list the list are inserting reversely.每次插入时都会得到相同的索引,最后一个插入元素会移动到index+1 position,这就是为什么Jamies_list列表似乎反向插入的原因。

Second code snippet第二个代码片段

Since you're allocating len(list2) every time of for loop , it get dynamically update the len and taking the new the position of the new item由于您每次for loop都分配len(list2) ,因此它会动态更新len并获取新项目的新 position

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

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