在Python中,用方括号[] 表示列表,用逗号分隔其中的元素。

Code
Country = ["China",'USA',"UK","France","German","Japan"]
print(Country)
['China', 'USA', 'UK', 'France', 'German', 'Japan']

3.1 访问元素

索引从0开始,而不是从1开始。

Code
print(Country[0])
print(Country[4])
China
German

也可以从倒数第一个数向前访问

Code
print(Country[-1])
print(Country[-2])
print(Country[-3])
Japan
German
France

使用列表中的值

Code
intro = f"Hello,I'm from {Country[0]}!"
print(intro)
Hello,I'm from China!

3.2 修改、添加和删除元素

3.2.1 修改元素

Code
num = [1,2,3,4,5,6,7,8,9]
print(num)

num[0] = 0
print(num)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 2, 3, 4, 5, 6, 7, 8, 9]

3.2.2 添加元素

  1. 在列表末尾添加元素

    追加(append())到列表末尾

    Code
    num = [1,2,3,4,5,6,7,8,9]
    print(num)
    
    num.append(10)
    print(num)
    [1, 2, 3, 4, 5, 6, 7, 8, 9]
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

    可以先创建空列表,再用append逐个添加元素。

    Code
    fruit = []
    fruit.append("apple")
    fruit.append("pear")
    
    print(fruit)
    ['apple', 'pear']
  2. 在列表中插入元素

    使用insert()方法可在列表的任意位置添加元素

    Code
    num = [1,2,3,4,5,6,7,8,9]
    print(num)
    num.insert(0,0)
    num
    [1, 2, 3, 4, 5, 6, 7, 8, 9]
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

3.2.3 删除元素

  1. 使用del 语句删除元素

    Code
    num = [1,2,3,4,5,6,7,8,9]
    
    del num[0]
    del num[2]
    num
    [2, 3, 5, 6, 7, 8, 9]
  2. 使用pop()方法删除元素

    弹出pop() 方法删除列表末尾的元素

    Code
    num = [1,2,3,4,5,6,7,8,9]
    popped_num = num.pop()
    num
    popped_num
    9
  3. 删除任意位置的元素

    也可以使用pop()方法删除列表中任意位置的元素,指定索引即可

    Code
    num = [1,2,3,4,5,6,7,8,9]
    fifth_num = num.pop(4)
    num
    [1, 2, 3, 4, 6, 7, 8, 9]
  4. 根据值删除元素

    Code
    num = [1,2,3,4,5,6,7,8,9]
    hate_num = 4
    num.remove(hate_num)
    num
    [1, 2, 3, 5, 6, 7, 8, 9]

3.3 管理列表

  1. 使用sort()方法永久排序

    Code
    ### 字母顺序
    letters = ["a","r","k","n","v","z"]
    letters.sort()
    print(letters)
    
    ### 字母逆序
    letters = ["a","r","k","n","v","z"]
    letters.sort(reverse = True)
    print(letters)
    ['a', 'k', 'n', 'r', 'v', 'z']
    ['z', 'v', 'r', 'n', 'k', 'a']
  2. 使用sorted()函数临时排序

    Code
    letters = ["a","r","k","n","v","z"]
    sorted(letters)
    sorted(letters,reverse = True)
    print(letters)
    ['a', 'r', 'k', 'n', 'v', 'z']
  3. 使用reverse()方法反向打印列表

    Code
    letters = ["a","r","k","n","v","z"]
    letters.reverse()
    print(letters) #反向打印
    letters.reverse()
    print(letters) #恢复
    ['z', 'v', 'n', 'k', 'r', 'a']
    ['a', 'r', 'k', 'n', 'v', 'z']
  4. 使用len()函数确定列表长度

    Code
    letters = ["a","r","k","n","v","z"]
    len(letters)
    6

3.4 遍历整个列表

for循环

注意避免缩进错误,如忘记缩进、忘记缩进额外的代码行、不必要的缩进、循环后不必要的缩进、遗漏冒号、

Code
subjects = ["Chinese","Mathematics","English","Physics","Chemistry","Biology"]
for subject in subjects:
  print(f"{subject}:")
  print(f"{subject} is very important for students.\n")
print("day day up!")
Chinese:
Chinese is very important for students.

Mathematics:
Mathematics is very important for students.

English:
English is very important for students.

Physics:
Physics is very important for students.

Chemistry:
Chemistry is very important for students.

Biology:
Biology is very important for students.

day day up!

3.5 创建数值列表

3.5.1 使用range()函数创建列表

Code
for value in range(-3,3):
  print(value)
-3
-2
-1
0
1
2
Code
numbers = list(range(-5,5,2))
numbers


squares = []
for value in range(1,11):
  square=value**2
  squares.append(square)
print(squares)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

3.5.2 统计函数

Code
digits = list(range(0,10))
min(digits)
max(digits)
sum(digits)
45

3.5.3 列表推导式list comprehension

Code
squares = [i**2 for i in range(1,10)]
squares
[1, 4, 9, 16, 25, 36, 49, 64, 81]

3.6 slice

列表的部分称为slice

Code
letters = ["a","r","k","n","v","z"]
# 左包含,右不包含
letters[0:3] 
letters[3:5]  
letters[:3]    # 0,1,2
letters[:-2]   # 从头到倒数第2之前1位
letters[2:]    # 从第3到末尾
letters[-3:]   # 从倒数第3到末尾

letters[::2] # 从头到尾每间隔2打印
letters[1::3]




# 遍历slice

for i in letters[:3]:
  i.upper()
  

# 复制slice
字母 = letters[:]  # 从头到尾
字母.append("X")
字母
letters.append("Y")
letters

#不使用slice   
letters = ["a","r","k","n","v","z"]
letters
LETTERS = letters         # 两种指向同一个列表
LETTERS.append("X")
LETTERS
letters
letters.append("Y")     
LETTERS
letters
['a', 'r', 'k', 'n', 'v', 'z', 'X', 'Y']

3.7 元组tuple

Python将不能修改的值称为不可变的。而不可变的列表称为元组(tuple)。元组使用圆括号()+逗号,标识。严格地说,元组是用逗号标识的,圆括号只是让元组看起来更整洁、更清晰,并不是必须的。

Code
dimensions = (200,50,4)
dimensions
# dimensions[0] = 333 # 'tuple' object does not support item assignment

single = 9,
# single[0] = 3

虽然元组的元素不能修改,但可以重新给表示元组的变量赋值

Code
dimensions = (200,50,4)
dimensions
dimensions = 2,3,4,5
dimensions
for i in dimensions:
  i