zoukankan      html  css  js  c++  java
  • Python-列表 元组-list tuple

    列表 list
      [vale,...] 可加入任意类型数据,并可嵌套,不定长

    student = ["beimenchuixue", "maYun", "maHuiTeng", "naiChaDong"]
    product = ["blog", "taoBao", "weiChat", "JD"]
    score = ["beimenchuixue", [90, 80, 66, 77, 99]]

    列表访问
      1. 指定插入 .insert
      2. 末尾插入 .append
      3. 知值删 .remove
      4. 知索引删,并返回值 .pop del
      5. 清空列表 .clear
      6. 反转列表 .reverse
      7. 排序列表 .sort
      8. 扩展列表 .extend
      9. 拷贝列表 .copy
      10. 知值查索引 index
      11. 定义空列表 [] list()

    product = ["boKeYuan", "taoBao", "weiChat", "jd", "baiDuYun", "aliYun"]
    product.insert(1, "jinRiTouTiao")
    print(product)
    
    product.append("douYin")
    print(product)
    
    product.remove("jd")
    print(product)
    
    del_product = product.pop(0)
    print(del_product)
    print(product)
    
    product.reverse()
    print(product)
    
    # product.sort(lambda x: x[0], )
    # print(product)
    
    two_product = ["weiRuan", "Centos", "Nginx"]
    product.extend(two_product)
    print(product)
    
    copy_product = product.copy()
    print(copy_product)
    
    print(copy_product.index("aliYun"))
    
    copy_product.clear()
    print(copy_product)
    
    empty_list = []
    empty_list_two = list()
    print(empty_list, empty_list_two)
    
    print(product.count("aliYun"))
    
    
    tuple_product = ("boKeYuan", "taoBao", "weiChat", "jd", "baiDuYun", "aliYun")
    print(tuple_product.index("weiChat"))
    print(tuple_product.count("weiChat"))

    元组 tuple
      (value, ...)
      1. 声明数据不可变,避免隐藏的错误,固定不变的数据推荐使用元组
      2. 可加入任意类型数据,并可嵌套,单个值需要加上 ,
      3. 定义空元组 () tuple()

    tuple_product = ("boKeYuan", "taoBao", "weiChat", "jd", "baiDuYun", "aliYun")
    print(tuple_product.index("weiChat"))
    print(tuple_product.count("weiChat"))

       4. 不可变性是相对的

    names = ("BeiMenChuiXue", [18, 175])
    names[1][1] = 180
    print(names)
    

       5. 拆包,隐含位置信息

    name, age, height = ("BeiMenChuiXue", 18, 175)
    print(name, age, height)
    name, *other = ("BeiMenChuiXue", 18, 175)
    print(name, other)
    

      

    tuple 比 list好的地方
      1.不可变对象
        a. 性能优化(作为常量在编译时确认)
        b. 线程安全
        c. 可以作为dict的key, 可hash对象才可以做字典key
        d. 拆包特性
      2. 用C语言做类别,Tuple对应的struct,而list对应则是array

    () 的意思
      1. 强制或强调优先级顺序
      2. 数学运算 type((5)) 结果为 int
      3. 空元组 () 和元组 (value, ...)
      4. 生成器 () + for
      5. 正则表达式当作一个字符,并取其匹配的字符 (re)

    result = not True or True
    
    result_two = not (True or True)
    print(result, result_two)
    
    
    print(type((6 + 7)))
    
    print(type(()))
    
    generator = (i for i in range(10))
    print(generator.__next__())
    
    one_str = "Simple is better than complex."
    regular = r"w+"
    import re
    find_list = re.findall(regular, one_str)
    print(find_list)
    
  • 相关阅读:
    sqlplus edit 方式设置成vi
    oracle minus union intersect
    子查询中可以包含order by 子句--(在from里面)
    Linux 7 Ansible 初学 一个简单的 playbook 学习 yum 模块
    Linux 7 Ansible 初学 配置被控制机器的 YUM 源
    Linux 7 安装 Ansible 并作基本的配置
    Linux 7 安装开发工具包 Development Tools
    Linux 7.0 安装 mariadb 数据库及初始化,创建数据库,创建用户
    Linux bash初学 if语句
    Linux bash初学 case语句
  • 原文地址:https://www.cnblogs.com/2bjiujiu/p/9061911.html
Copyright © 2011-2022 走看看