[英]Difference between foo=list, foo=[], and foo=list()?
I'm new to python (but not programming in general) and don't really understand what is going on with lists. 我是python的新手(但不是一般的编程人员),并且真的不了解列表的内容。 I have thousands of test sets in a text file and each test set is 256 (could be dfferent) x,y points.
我在一个文本文件中有成千上万个测试集,每个测试集是256(可以是不同的)x,y点。 What I want to do is store each point in a test set to a list and then store each list in another list, effectively creating a 2D list.
我要做的是将测试集中的每个点存储到一个列表中,然后将每个列表存储在另一个列表中,从而有效地创建2D列表。 Two of the three options I've tried are error free but I don't know what the different options mean or why I should pick one over another.
我尝试过的三个选项中的两个是无错误的,但是我不知道不同的选项是什么意思,也不知道为什么我应该选择一个。 This is not the same as "Least Astonishment" and the Mutable Default Argument because that doesn't explain which of these is a list and what the other definitions are.
这与“最小惊讶”和“可变默认参数”不同,因为这不能解释其中的哪个是列表以及其他的定义是什么。
foo= list
foo.append(1)#TypeError: descriptor 'append' requires a 'list' object but received a 'int'
bar= []
bar.append(1)#no error
baz= list()
baz.append(1)#no error
The first one sets foo
to the object list
. 第一个将
foo
设置为对象list
。 This is not an empty list, as you've discovered. 正如您所发现的,这不是一个空列表。 The object
list
is a built-in type ... and you now have an in-scope alias to the type. 对象
list
是内置类型...现在您具有该类型的作用域内别名。 You cannot append to a type; 您不能追加类型; hence the error message.
因此错误消息。
The other two are classic ways to initialize a variable to an empty list. 另外两种是将变量初始化为空列表的经典方法。 The middle one uses list literal notation to construct a list, while the last one invokes the
list
type object as a function to generate an instance of the type. 中间一个使用列表文字表示法构造一个列表,而最后一个使用
list
类型对象作为函数来生成该类型的实例。
When you're doing foo = list
, you're actually assigning foo
to a built-in python object of list. 当您执行
foo = list
,实际上是将foo
分配给foo = list
的内置python对象。 bar = list()
and baz = []
both initiates two new variables as new vacant list. bar = list()
和baz = []
都将启动两个新变量作为新的空缺列表。 However in python var_ = []
assigning is a bit faster compared to var_ = list()
. 但是在python中,
var_ = []
分配要比var_ = list()
快一些。
This is because foo = list
makes foo
an alias for the list
type. 这是因为
foo = list
使foo
成为list
类型的别名。 No actual list
object has been instantiated in that line. 该行中没有实例化任何实际的
list
对象。 However, if you have a list
object, you can use list.append()
(or foo.append()
) in order to append to it. 但是,如果您有一个
list
对象,则可以使用list.append()
(或foo.append()
)来附加到该对象。
Like so: 像这样:
foo = list
bar = [1]
list.append(bar, 2)
# bar is now [1, 2]
foo.append(bar, 3)
# bar is now [1, 2, 3]
Since your example code calls foo.append()
with only one argument (an int), the exception complains that it needs a list
and not an int
. 由于您的示例代码仅使用一个参数(一个int)调用
foo.append()
,因此该异常会抱怨它需要一个list
而不是一个int
。
Note: This is not a special feature of the list class. 注意:这不是列表类的特殊功能。 All instance methods can be called in both forms:
some_object.func(a, b, c)
or SomeType.func(some_object, a, b, c)
可以两种形式调用所有实例方法:
some_object.func(a, b, c)
或SomeType.func(some_object, a, b, c)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.