zoukankan      html  css  js  c++  java
  • python-列表和元组

    1. 列表

    列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储、修改等操作

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # Auther: Summer
    
    
    # 定义列表
    names
    = ["A", "B", "C", "D", "E", "F"] # 切片 print(names[0]) print(names[0:2]) # 列表用前闭后开的形式取值 print(names[0:-1]) print(names[:-1]) print(names[-1]) print(names[0:-1:2]) print(names[::2]) # 0和-1可以默认不写 # 追加 names.append("summer") # 追加至末尾 print(names) # 插入(可插入至前排)
    # 一次只能插入一个,不可同时插入多个元素
    names.insert(1, "mayun") print(names) names.insert(2, "强势围观马云") print(names) # 修改(重新赋值) names[1] = "马爸爸该换人了" print(names) # 删除 del names[0] print(names) names.remove("B") # 移除指定元素 print(names) names.pop() # 默认删除最后一个值 print(names) names.pop(0) # 删除指定位置的值 print(names) # 扩展 b = [1,2,3] names.extend(b) # 扩展至末尾 print(names, b) # 拷贝 # 深拷贝和浅拷贝,还需要细看 names_copy = names.copy() print(names_copy) names.insert(3,["1", "2", "3"]) print(names) names[3][2] = "dd" names[1] = "nihao" # names_copy = names.copy() print(names_copy) print(names) # 统计 names.append(1) print(names) print(names.count(1)) # 排序和翻转 names = ["A", "B", "C", "D", "E", '马爸爸该换人了', '强势围观马云', 'B', 'C', 'D', "F"] names.sort() # python3里不同类型数据不能放在一起排序了!!!(int and str) print(names) # print(names.sort()) # 直接print(names.sort())结果是none 需要先调用然后再print(names)就OK names = ['Amy', 'Tenglan', 'Eom'] names.reverse() print(names) # 获取下标 names = ["A", "B", "C", "D", "E", '马爸爸该换人了', '强势围观马云',"B", "C", "D", "F"] num = names.index("D") # 只返回找到的第一个坐标 print(num)

    2.元组:

    元组又叫只读列表,不可更改,只有两个方法count和index 只能查询
    # 定义元组
    names = ("alex","jack","eric")
  • 相关阅读:
    Linux YUM (Yellowdog Updater, Modified) Commands for Package Management
    awk命令例子详解
    sed命令例子详解
    浅谈XX系统跨平台迁移(测试环境)
    Postgres常用命令之增、删、改、查
    Postgres主备切换
    Postgres配置主从流复制
    Crontab定时任务
    GIL全局锁测试
    python try exception finally记录
  • 原文地址:https://www.cnblogs.com/summerxye/p/8022929.html
Copyright © 2011-2022 走看看