
# 20.列表
- # 一维列表
- names = ['Hash', 'Bob', 'Nick']
- print(names) # 全打印
- print(names[:]) # 全打印
- print(names[1:3]) # 打印1到2号索引
- print(names[:2]) # 打印0到1号索引
- '''
- ['Hash', 'Bob', 'Nick']
- ['Hash', 'Bob', 'Nick']
- ['Bob', 'Nick']
- ['Hash', 'Bob']
- '''
- # 二维列表:一维列表中嵌套一维列表
- matrix = [
- [1, 2, 1, 4],
- [5, 2, 0],
- ['I', 'love']
- ]
- print(matrix[2][1]) # love
-
- matrix[0][1] = 3 # 修改列表元素
- print(matrix[0][1]) # 3
-
- print(matrix) # [[1, 3, 1, 4], [5, 2, 0], ['I', 'love']]
-
- for row in matrix: # 遍历列表元素
- print(row)
- for item in row:
- print(item)
- '''
- [1, 3, 1, 4]
- 1
- 3
- 1
- 4
- [5, 2, 0]
- 5
- 2
- 0
- ['I', 'love']
- I
- love
- '''
// 练习:查询一个列表中最大的数
- nums = [12, 34, 112, 45, 54, 21]
- num_max = nums[0]
- for num in nums:
- if num_max < num:
- num_max = num
- print(num_max) # 112
# 21.列表常用方法
- num = [5, 2, 12, 45, 16, 2]
- num.append(100) # 在列表末尾添加100
- num.append('sd') # 在列表末尾添加sd
- print(num) # [5, 2, 12, 45, 16, 2, 100, 'sd']
-
- num.insert(0, 0) # 在0号索引添加元素0
- print(num) # [0, 5, 2, 12, 45, 16, 2, 100, 'sd']
-
- num.remove(2) # 删除第一个2
- print(num) # [0, 5, 12, 45, 16, 2, 100, 'sd']
-
- num.pop() # 删除末尾项
- print(num) # [0, 5, 12, 45, 16, 2, 100]
-
- print(num.index(2)) # 5, 查询元素2在列表中的索引位置
- print(num.count(5)) # 1, 统计字符5在列表中的个数
-
- num.sort() # 升序排序
- print(num) # [0, 2, 5, 12, 16, 45, 100]
-
- num.reverse() # 降序排序
- print(num) # [100, 45, 16, 12, 5, 2, 0]
-
- num2 = num.copy() # 复制num列表到num2列表中
- print(num2) # [100, 45, 16, 12, 5, 2, 0]
-
- num.clear() # 清空列表
- print(num) # []
// 练习:创立一个空列表,将一个非空列表中的非重复元素逐个复制进空列表中。
- empty_list = []
- copied_list = [12, 12, 34, 23, 21, 34, 4]
-
- for copy in copied_list:
- if copy not in empty_list:
- empty_list.append(copy)
-
- empty_list.sort()
- print(empty_list) # [4, 12, 21, 23, 34]
# 22.元组
# 元组类似列表,但里面的元素是不可改变的,也不能增减。(适合放置固定值)
- yuan_zu = (1, 2, 3, 4)
- print(yuan_zu) # (1, 2, 3, 4)
-
- # yuan_zu[1] = 10
- # print(yuan_zu[1]) # TypeError: 'tuple' object does not support item assignment
# 23.拆包
- coordinates = (5, 6, 7)
- x = coordinates[0]
- y = coordinates[1]
- z = coordinates[2]
- print(x, y, z) # 5 6 7
-
- # 上述赋值可简化为如下所示
- new_x, new_y, new_z = coordinates
- print(new_x, new_y, new_z) # 5 6 7
# 24. 字典
# 注意事项:每个单词只设置一次,重复设置会被覆盖; 字典的单词需要""括起来。
- dictionary = {
- "name": 'xiaoxiao',
- "age": 12,
- "is_verified": True,
- "name": 'shanghai'
- }
- print(dictionary["name"]) # shanghai
-
- # 使用get访问字典元素,即使不存在该元素也不会报错。
- print(dictionary.get("name")) # shanghai
- print(dictionary.get("name")) # shanghai
-
- print(dictionary.get("id")) # None
-
- print(dictionary.get("ID", 12345678)) # 12345678, 使用get方法可创建新元素
- print(dictionary.get("name", "new_name")) # shanghai, 但已存在的元素值无法使用get方法修改,更安全。
-
- dictionary["name"] = "my_home"
- print(dictionary["name"]) # my_home
# 练习:输入一串数字,并将0~9转换为英文(也可自行转换为其他字符,了解密码学的基本原理)
- digits_mapping = {
- "0": 'zero',
- "1": 'one',
- "2": 'two',
- "3": 'three',
- "4": 'four',
- "5": 'five',
- "6": 'six',
- "7": 'seven',
- "8": 'eight',
- "9": 'nine'
- }
-
- phone = input("Phone: ")
- output = ""
-
- # for eng in phone:
- # output += digits_mapping.get(eng) + " "
- # print(output)
- # TypeError: unsupported operand type(s) for +: 'NoneType' and 'str',空值不能与字符相加。
- # 解决方案:给不在字典中的单词设置一个默认值。
-
- for eng in phone:
- output += digits_mapping.get(eng, "not_digit") + " "
- print(output)
-
- """
- Phone: 123141235466xx
- one two three one four one two three five four six six not_digit not_digit
- """
# 25.表情转换器
- message = input("> ")
- words = message.split() # 默认以空格为分割符,将字符串分组。
- print(words)
- """
- > "Hello, welcome to my world."
- ['"Hello,', 'welcome', 'to', 'my', 'world."']
- """
- emojis = {
- ":)": "😊",
- ":(": "😥",
- "pig": "🐷"
- }
- output = ""
- for word in words:
- output += emojis.get(word, word) + " "
- print(output)
- """
- > the pig is :) ,but the cat is :(
- ['the', 'pig', 'is', ':)', ',but', 'the', 'cat', 'is', ':(']
- the 🐷 is 😊 ,but the cat is 😥
- """