[英]Modifying a list to copy another without making a copy of the original
I have a program which is an implementation of a sorting algorithm 我有一个程序是排序算法的实现
def myfunction(data):
x = [sorted elements...]
Input 输入项
mylist = [elements...]
myfunction(mylist)
print(mylist)
The function ends up eventually with a list x, with the same elements which have been sorted, but is a copy of mylist. 该函数最终以列表x结束,该列表具有已排序的相同元素,但是是mylist的副本。 This means that when the program is run, mylist is returned, instead of x. 这意味着在运行程序时,将返回mylist而不是x。
How can I alter mylist within myfunction so that is the same as x? 如何更改myfunction中的mylist,使其与x相同? Surely there is a way to map x and mylist to each other and then iteratively alter mylist until it matches x? 当然有一种方法可以将x和mylist相互映射,然后迭代更改mylist直到匹配x?
def myfunction(data):
x = [sorted elements...]
data[:] = x
This modifies data
in-place and sets it to whatever values x
contains. 这将就地修改data
并将其设置为x
包含的任何值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.