我正在分析一些Python代码,我不知道是什么
pop = population[:]
Run Code Online (Sandbox Code Playgroud)
手段.它是像Java中的数组列表还是像二维数组?可以感谢一些帮助,谢谢.
Python doc说切片列表会返回一个新列表.现在,如果返回"新"列表,我有以下与"分配给切片"有关的疑问
a = [1, 2, 3]
a[0:2] = [4, 5]
print a
Run Code Online (Sandbox Code Playgroud)
现在输出将是:
[4, 5, 3]
Run Code Online (Sandbox Code Playgroud)
我正在一个非常依赖性能的应用程序上使用 python 3.5 列表。
在字典列表中搜索对象删除后,我想知道该解决方案中的冒号[:]。我知道它创建了一个副本(理论上),但在这个例子中看不到它的目的。
它可能节省内存吗?或者它与浅拷贝/深拷贝有关吗?对于现有的情况和现有的情况,python 的行为会有所不同mylist = ...吗mylist[:] = ...?
我发现了一些类似的问题,但没有一个问题涉及 python 在给定这样的任务时的行为方式。
I'm currently learning Python and I encountered an issue with with assignment to a list.
In
def nextPermutation(self, nums: List[int]) -> None:
...
Run Code Online (Sandbox Code Playgroud)
I have a line of code that reverses the list as follows:
nums = nums[::-1]
Run Code Online (Sandbox Code Playgroud)
but the test bench marks it as incorrect, whereas
nums[:] = nums[::-1]
Run Code Online (Sandbox Code Playgroud)
is ok.
I suspect it's because I'm creating another nums list object and the original list that's passed in is unchanged, is that right?