In python documentation list is defined as:
在python文档列表中定义为:
mutable sequences, typically used to store collections of homogeneous items (where the precise degree of similarity will vary by application).
易变序列,通常用于存储同构项的集合(其相似性的精确程度因应用程序而异)。
Why it's used to store collections of homogeneous items?
为什么它被用来存储同类产品的集合?
Are a string and an int item also homogeneous then?
那么字符串和int项也是同构的吗?
a = [12,"hello"]
3 个解决方案
#1
11
Homogeneous means "of the same or a similar kind or nature".
同质的意思是“相同或相似的种类或性质”。
While any value can be stored in a list along with any other value, in doing so the definition of "kind or nature" must be widened when dealing with the sequence. During this widening (or "unification"), the set of operations that can be performed upon every item in the sequence becomes the "lowest common set of operations" shared between all items.
虽然任何值都可以与任何其他值一起存储在列表中,但是在处理序列时,必须扩大“类或性质”的定义。在此扩展(或“统一”)期间,可以对序列中的每个项执行的操作集成为所有项之间共享的“最低公共操作集”。
This is why "[list are] typically used to store collections of homogeneous items" - so the items in the sequence can be treated with an appropriate level of unification:
这就是为什么“[list]通常用于存储同类商品的集合”——因此序列中的项目可以用适当的统一级别来处理:
# a list of animals that can "speak"
animals = [Dog(), Cat(), Turkey()]
for a in animals:
a.speak()
# .. but a string cannot "speak"
animals = [Dog(), "Meow!", Turkey()]
#2
2
While you technically can store any object in a list:
技术上来说,你可以在列表中存储任何对象:
[12, "hello", list, list()]
Lists are, as the documentation says, usually used to store similar items:
如文件所述,列表通常用于存储类似的项目:
[12, 24, 99]
["hello", "goodbye"]
[list, dict, int]
The meaning of "homogenous" is simply "similar".
“同质”的意思就是“相似”。
#3
2
It talks about a common use case (that's why it says "typically"). Homogeneity is neither expected nor enforced, as illustrated by the example in your question. Even what it means for items to be "homogenous" is not precisely defined: the doc says that this "will vary by application".
它讨论一个常见的用例(这就是为什么它说“典型”)。同质性既不是预期的,也不是强制的,如您问题中的示例所示。甚至对于“同质的”物品的含义也没有明确的定义:doc说这“会因应用而异”。
#1
11
Homogeneous means "of the same or a similar kind or nature".
同质的意思是“相同或相似的种类或性质”。
While any value can be stored in a list along with any other value, in doing so the definition of "kind or nature" must be widened when dealing with the sequence. During this widening (or "unification"), the set of operations that can be performed upon every item in the sequence becomes the "lowest common set of operations" shared between all items.
虽然任何值都可以与任何其他值一起存储在列表中,但是在处理序列时,必须扩大“类或性质”的定义。在此扩展(或“统一”)期间,可以对序列中的每个项执行的操作集成为所有项之间共享的“最低公共操作集”。
This is why "[list are] typically used to store collections of homogeneous items" - so the items in the sequence can be treated with an appropriate level of unification:
这就是为什么“[list]通常用于存储同类商品的集合”——因此序列中的项目可以用适当的统一级别来处理:
# a list of animals that can "speak"
animals = [Dog(), Cat(), Turkey()]
for a in animals:
a.speak()
# .. but a string cannot "speak"
animals = [Dog(), "Meow!", Turkey()]
#2
2
While you technically can store any object in a list:
技术上来说,你可以在列表中存储任何对象:
[12, "hello", list, list()]
Lists are, as the documentation says, usually used to store similar items:
如文件所述,列表通常用于存储类似的项目:
[12, 24, 99]
["hello", "goodbye"]
[list, dict, int]
The meaning of "homogenous" is simply "similar".
“同质”的意思就是“相似”。
#3
2
It talks about a common use case (that's why it says "typically"). Homogeneity is neither expected nor enforced, as illustrated by the example in your question. Even what it means for items to be "homogenous" is not precisely defined: the doc says that this "will vary by application".
它讨论一个常见的用例(这就是为什么它说“典型”)。同质性既不是预期的,也不是强制的,如您问题中的示例所示。甚至对于“同质的”物品的含义也没有明确的定义:doc说这“会因应用而异”。