python_列表、元组、字典、集合对比

列表、元组、字典、集合

  • 列表、元组、字典、集合对比

    比较项 列表 元组 字典 集合
    类型名称 list tuple dict set
    定界符 [] () {} {}
    是否可变
    是否有序
    是否支持下标 是(使用序号作为下标) 是(使用序号作为下标) 是(使用"键"作为下标)
    元素分隔符 逗号 逗号 逗号 逗号
    对元素形式的要求 键:值 必须可哈希
    对元素值的要求 "键"必须可哈希 必须可哈希
    元素是否可重复 "键":不允许重复<br />"值":可以重复
    元素查找速度 非常慢 很慢 非常快 非常快
    新增和删除元素速度 尾部操作快,其它位置慢 不允许
  • 实例

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    x_list = [1, 3, 2, 5]  # 创建列表对象
    x_tuple = (1, 2, 3)  # 创建元组对象
    x_dict = {'a': 97, 'b': 98, 'c': 99}  # 创建字典对象
    x_set = {1, 2, 3}  # 创建集合对象
    print(x_list[2])
    print(x_tuple[1])
    print(x_dict['b'])
    print(3 in x_set)
    # 执行结果
    D:\pycode\python_s3\venv\Scripts\python.exe D:/pycode/python_s3/day10/list_tuple_dict_set.py
    2
    2
    98
    True

 

猜你喜欢

转载自www.cnblogs.com/zxbdboke/p/10468209.html