问题:这是什么意思?

我正在分析一些Python代码,但我不知道

pop = population[:]

手段。是Java中的数组列表还是二维数组?

I’m analyzing some Python code and I don’t know what

pop = population[:]

means. Is it something like array lists in Java or like a bi-dimensional array?


回答 0

它是切片符号的示例,其作用取决于的类型population。如果population是列表,则此行将创建列表的浅表副本。对于类型为tuple或的对象str,它将不执行任何操作(如果不使用,该行将执行相同操作[:]),对于(例如)NumPy数组,它将为相同的数据创建一个新视图。

It is an example of slice notation, and what it does depends on the type of population. If population is a list, this line will create a shallow copy of the list. For an object of type tuple or a str, it will do nothing (the line will do the same without [:]), and for a (say) NumPy array, it will create a new view to the same data.


回答 1

了解列表切片通常会复制一部分列表,这也可能会有所帮助。例如,population[2:4]将返回一个包含人口[2]和人口[3]的列表(切片是右排它的)。保留左和右索引,因为population[:]它们分别默认为0和length(population),从而选择了整个列表。因此,这是制作列表副本的常见习语。

It might also help to know that a list slice in general makes a copy of part of the list. E.g. population[2:4] will return a list containing population[2] and population[3] (slicing is right-exclusive). Leaving away the left and right index, as in population[:] they default to 0 and length(population) respectively, thereby selecting the entire list. Hence this is a common idiom to make a copy of a list.


回答 2

好吧…这确实取决于上下文。最终,它通过一个对象(slice(None,None,None))为以下方法之一: __getitem____setitem____delitem__。(实际上,如果对象具有__getslice__,将代替__getitem__,但现在已弃用,不应使用)。

对象可以对切片执行其所需的操作。

在以下情况下:

x = obj[:]

这将obj.__getitem__使用传入的slice对象进行调用。实际上,这完全等效于:

x = obj[slice(None,None,None)]

(尽管前者可能更有效,因为它不必查找 slice构造函数-全部用字节码完成)。

对于大多数对象,这是一种创建序列的一部分的浅表副本的方法。

下一个:

x[:] = obj

是一种设置项目的方法(它调用 __setitem__基于)的方法obj

而且,我想您可能会猜出什么:

del x[:]

调用;-)。

您还可以传递不同的切片:

x[1:4]

结构体 slice(1,4,None)

x[::-1]

构造slice(None,None,-1)等等。进一步阅读: 解释Python的切片符号

well… this really depends on the context. Ultimately, it passes a object (slice(None,None,None)) to one of the following methods: __getitem__, __setitem__ or __delitem__. (Actually, if the object has a __getslice__, that will be used instead of __getitem__, but that is now deprecated and shouldn’t be used).

Objects can do what they want with the slice.

In the context of:

x = obj[:]

This will call obj.__getitem__ with the slice object passed in. In fact, this is completely equivalent to:

x = obj[slice(None,None,None)]

(although the former is probably more efficient because it doesn’t have to look up the slice constructor — It’s all done in bytecode).

For most objects, this is a way to create a shallow copy of a portion of the sequence.

Next:

x[:] = obj

Is a way to set the items (it calls __setitem__) based on obj.

and, I think you can probably guess what:

del x[:]

calls ;-).

You can also pass different slices:

x[1:4]

constructs slice(1,4,None)

x[::-1]

constructs slice(None,None,-1) and so forth. Further reading: Explain Python’s slice notation


回答 3

这是一片从序列开始到结束的,通常会产生浅拷贝。

(嗯,不仅限于此,但您无需关心。)

It is a slice from the beginning of the sequence to the end, usually producing a shallow copy.

(Well, it’s more than that, but you don’t need to care yet.)


回答 4

它创建列表的副本,而不仅仅是为现有列表分配新名称。

It creates a copy of the list, versus just assigning a new name for the already existing list.


回答 5

[:]
用于限制器或数组中的切片,散列,
例如:
[1:5]用于显示1到5之间的值,即1-4
[start:end]

主要用于数组中的切片,理解方括号接受表示的变量要显示的值或键,“:”用于将整个数组限制或分割为数据包。

[:]
used for limiter or slicing in array , hash
eg:
[1:5] for displaying values between 1 inclusive and 5 exclusive i.e 1-4
[start:end]

basically used in array for slicing , understand bracket accept variable that mean value or key to display, and ” : ” is used to limit or slice the entire array into packets .


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。