第五章 数据结构1
第五章 数据结构1
相关代码如下
letters = ["a", "b", "c"]
matrix = [[0, 1], [2, 3]]
zeros = [0] * 5
combined = zeros + letters
numbers = list(range(20)) # range()函数可遍历20,list()函数可将遍历的内容化为数组
chars = list("Hello World")
print(chars)
print(len(chars)) # len()函数可计算对象的内容个数
print("---------------------------------")
letters = ["a", "b", "c", "d"]
letters[0] = "A"
print(letters[-1])
print(letters[::-1]) # 输出数组时,第三个空可填写每个内容相隔个数,若为负数则可输出反方向
numbers = [1, 2, 3, 9]
first, *other, last = numbers
print(first, last)
print(other)
# 拆包,星号+变量 可将剩余打包成列表
print("---------------------------------")
letters = ["a", "b", "c"]
for index, letter in enumerate(letters): # enumerate可将列表里的内容一一对应于元组形式
print(index, letter)
print("---------------------------------")
输出内容
['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
11
---------------------------------
d
['d', 'c', 'b', 'A']
1 9
[2, 3]
---------------------------------
0 a
1 b
2 c
---------------------------------