- Dec 2020
-
runestone.academy runestone.academy
-
It is called a sparse matrix.
稀疏矩阵
-
-
runestone.academy runestone.academy
-
Aliasing and Copying
别名和复制
-
-
runestone.academy runestone.academy
-
Dictionary Operations
操作符
-
-
runestone.academy runestone.academy
-
One way to create a dictionary is to start with the empty dictionary and add key-value pairs. The empty dictionary is denoted {}
创建字典的一种方法是从空字典开始并添加键-值对。空字典表示为{}
-
mapping type
映射类型
-
-
runestone.academy runestone.academy
-
the last item of the list.
如果没有参数就删除最后一位
-
The dot operator
用来访问method
-
-
runestone.academy runestone.academy
-
Repetition and References
重复和引用
-
-
runestone.academy runestone.academy
-
Cloning Lists
克隆列表用切片 b = a【:】
-
a new list
切片会创造一个新的list
-
If we want to modify a list and also keep a copy of the original, we need to be able to make a copy of the list itself, not just the reference. This process is sometimes called cloning,
如果我们想修改一个列表,同时保留原始列表的副本,我们需要能够复制列表本身,而不仅仅是引用。这个过程有时被称为克隆,
-
-
runestone.academy runestone.academy
-
Changes made with one alias affect the other
a = 1 b = a 这时候 a b = 1
b = 2 print(a) a = 2
a的值会跟随着吧的变化而变化
-
Because the same list has two different names, a and b, we say that it is aliased
因为同一个列表有两个不同的名称,a和b,我们说它有别名
-
Aliasing
别名 把a 赋值给b 这样a b 引用同一个object
-
-
runestone.academy runestone.academy
-
a and b have the same value but do not refer to the same object.
list:a和b有相同的值,但不引用相同的对象 string : 是同一个object
-
The is operator will return true if the two references are to the same object
如果两个引用指向同一个对象,则is操作符将返回true
-
Objects and References
对象和引用
-
-
runestone.academy runestone.academy
-
We can even insert elements into a list by squeezing them into an empty slice at the desired location
利用切片【1:1】来插入值
-
We can also remove elements from a list by assigning the empty list to them.
可以用【】来remove list里面的元素
-
assignment with the slice operator
切片和赋值的结合
-
Lists are Mutable
列表是可变的
-
-
runestone.academy runestone.academy
-
List Slices
列表分割:截取list
-
-
runestone.academy runestone.academy
-
In Python, every object has a unique identification tag
在Python中,每个对象都有一个唯一的标识标签
每个object 在堆内存里都有一个id 空间
-
it refers to a completely new list
create one new list 就像 new string 一样,以前的list和string 还在
-
Concatenation and Repetition
list用 + 连接2个或多个list 用* 使list重复出现
-
-
runestone.academy runestone.academy
-
[56, 57, "dog"],
只有顶级项目返回True。57在子列表中。
-
in and not in
in and not in 判断给的object是否在 list里面 in : true not in : false
-
List Membership
列表的隶属关系
-
-
runestone.academy runestone.academy
-
alist[2][0])
print(alist[2] [0]) 先取index = 2 “cat“” 再 取cat 的第一位
-
Accessing Elements
访问元素 负索引值将从右侧而不是从左侧定位项。
-
The expression inside the brackets specifies the index.
【】里面是index
-
-
runestone.academy runestone.academy
-
[ ]
empty list is also one index
-
List Length
列表长度
-
sublists are considered to be a single item when counting the length of the list.
子list只算一个
-
-
runestone.academy runestone.academy
-
It is called the empty list and is denoted []
1.可以有子列表 2可以有空列表
-
-
runestone.academy runestone.academy
-
Lists are similar to strings, which are ordered collections of characters, except that the elements of a list can have any type and for any one list, the items can be of different types.
列表类似于字符串,字符串是有序的字符集合,不同之处在于列表的元素可以是任何类型,而且对于任何一个列表,项可以是不同类型。
-