• Python超入门(5)__迅速上手操作掌握Python


     

    # 20.列表
    
    1. # 一维列表
    2. names = ['Hash', 'Bob', 'Nick']
    3. print(names) # 全打印
    4. print(names[:]) # 全打印
    5. print(names[1:3]) # 打印1到2号索引
    6. print(names[:2]) # 打印0到1号索引
    7. '''
    8. ['Hash', 'Bob', 'Nick']
    9. ['Hash', 'Bob', 'Nick']
    10. ['Bob', 'Nick']
    11. ['Hash', 'Bob']
    12. '''
    1. # 二维列表:一维列表中嵌套一维列表
    2. matrix = [
    3. [1, 2, 1, 4],
    4. [5, 2, 0],
    5. ['I', 'love']
    6. ]
    7. print(matrix[2][1]) # love
    8. matrix[0][1] = 3 # 修改列表元素
    9. print(matrix[0][1]) # 3
    10. print(matrix) # [[1, 3, 1, 4], [5, 2, 0], ['I', 'love']]
    11. for row in matrix: # 遍历列表元素
    12. print(row)
    13. for item in row:
    14. print(item)
    15. '''
    16. [1, 3, 1, 4]
    17. 1
    18. 3
    19. 1
    20. 4
    21. [5, 2, 0]
    22. 5
    23. 2
    24. 0
    25. ['I', 'love']
    26. I
    27. love
    28. '''

    // 练习:查询一个列表中最大的数

    1. nums = [12, 34, 112, 45, 54, 21]
    2. num_max = nums[0]
    3. for num in nums:
    4. if num_max < num:
    5. num_max = num
    6. print(num_max) # 112

    # 21.列表常用方法
    1. num = [5, 2, 12, 45, 16, 2]
    2. num.append(100) # 在列表末尾添加100
    3. num.append('sd') # 在列表末尾添加sd
    4. print(num) # [5, 2, 12, 45, 16, 2, 100, 'sd']
    5. num.insert(0, 0) # 在0号索引添加元素0
    6. print(num) # [0, 5, 2, 12, 45, 16, 2, 100, 'sd']
    7. num.remove(2) # 删除第一个2
    8. print(num) # [0, 5, 12, 45, 16, 2, 100, 'sd']
    9. num.pop() # 删除末尾项
    10. print(num) # [0, 5, 12, 45, 16, 2, 100]
    11. print(num.index(2)) # 5, 查询元素2在列表中的索引位置
    12. print(num.count(5)) # 1, 统计字符5在列表中的个数
    13. num.sort() # 升序排序
    14. print(num) # [0, 2, 5, 12, 16, 45, 100]
    15. num.reverse() # 降序排序
    16. print(num) # [100, 45, 16, 12, 5, 2, 0]
    17. num2 = num.copy() # 复制num列表到num2列表中
    18. print(num2) # [100, 45, 16, 12, 5, 2, 0]
    19. num.clear() # 清空列表
    20. print(num) # []

    // 练习:创立一个空列表,将一个非空列表中的非重复元素逐个复制进空列表中。

    1. empty_list = []
    2. copied_list = [12, 12, 34, 23, 21, 34, 4]
    3. for copy in copied_list:
    4. if copy not in empty_list:
    5. empty_list.append(copy)
    6. empty_list.sort()
    7. print(empty_list) # [4, 12, 21, 23, 34]

    # 22.元组

    # 元组类似列表,但里面的元素是不可改变的,也不能增减。(适合放置固定值) 

    1. yuan_zu = (1, 2, 3, 4)
    2. print(yuan_zu) # (1, 2, 3, 4)
    3. # yuan_zu[1] = 10
    4. # print(yuan_zu[1]) # TypeError: 'tuple' object does not support item assignment

     

    # 23.拆包
    1. coordinates = (5, 6, 7)
    2. x = coordinates[0]
    3. y = coordinates[1]
    4. z = coordinates[2]
    5. print(x, y, z) # 5 6 7
    6. # 上述赋值可简化为如下所示
    7. new_x, new_y, new_z = coordinates
    8. print(new_x, new_y, new_z) # 5 6 7

     

    # 24. 字典
    # 注意事项:每个单词只设置一次,重复设置会被覆盖; 字典的单词需要""括起来。
    1. dictionary = {
    2. "name": 'xiaoxiao',
    3. "age": 12,
    4. "is_verified": True,
    5. "name": 'shanghai'
    6. }
    7. print(dictionary["name"]) # shanghai
    8. # 使用get访问字典元素,即使不存在该元素也不会报错。
    9. print(dictionary.get("name")) # shanghai
    10. print(dictionary.get("name")) # shanghai
    11. print(dictionary.get("id")) # None
    12. print(dictionary.get("ID", 12345678)) # 12345678, 使用get方法可创建新元素
    13. print(dictionary.get("name", "new_name")) # shanghai, 但已存在的元素值无法使用get方法修改,更安全。
    14. dictionary["name"] = "my_home"
    15. print(dictionary["name"]) # my_home
    # 练习:输入一串数字,并将0~9转换为英文(也可自行转换为其他字符,了解密码学的基本原理)
    1. digits_mapping = {
    2. "0": 'zero',
    3. "1": 'one',
    4. "2": 'two',
    5. "3": 'three',
    6. "4": 'four',
    7. "5": 'five',
    8. "6": 'six',
    9. "7": 'seven',
    10. "8": 'eight',
    11. "9": 'nine'
    12. }
    13. phone = input("Phone: ")
    14. output = ""
    15. # for eng in phone:
    16. # output += digits_mapping.get(eng) + " "
    17. # print(output)
    18. # TypeError: unsupported operand type(s) for +: 'NoneType' and 'str',空值不能与字符相加。
    19. # 解决方案:给不在字典中的单词设置一个默认值。
    20. for eng in phone:
    21. output += digits_mapping.get(eng, "not_digit") + " "
    22. print(output)
    23. """
    24. Phone: 123141235466xx
    25. one two three one four one two three five four six six not_digit not_digit
    26. """

     

    # 25.表情转换器
    1. message = input("> ")
    2. words = message.split() # 默认以空格为分割符,将字符串分组。
    3. print(words)
    4. """
    5. > "Hello, welcome to my world."
    6. ['"Hello,', 'welcome', 'to', 'my', 'world."']
    7. """
    8. emojis = {
    9. ":)": "😊",
    10. ":(": "😥",
    11. "pig": "🐷"
    12. }
    13. output = ""
    14. for word in words:
    15. output += emojis.get(word, word) + " "
    16. print(output)
    17. """
    18. > the pig is :) ,but the cat is :(
    19. ['the', 'pig', 'is', ':)', ',but', 'the', 'cat', 'is', ':(']
    20. the 🐷 is 😊 ,but the cat is 😥
    21. """
  • 相关阅读:
    MySQL8.0优化 - ER模型、数据表的设计原则
    Java HashSet集合概述
    UT804数据秒数据提取
    运维自动化之域名系统
    利用SSLsplit+arpspoof 实现ARP欺骗
    无聊话语罢了
    android 自定义View 视差动画
    青骨申报|CSC管理信息平台使用指南
    ESP32蓝牙实例-ESP32之间通过蓝牙串口主从机通信
    Mybatis
  • 原文地址:https://blog.csdn.net/qq_57233919/article/details/133894867