zoukankan      html  css  js  c++  java
  • python基础1--列表

    列表


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

    1.定义列表

    1 fruits = ['apple','banana','orange']

    2.通过下标访问列表中的元素,下标从0开始计数

    1 >>> fruits[0]
    2 'apple'
    3 >>> fruits[2]
    4 'orange'
    5 >>> fruits[-1]
    6 'orange'
    7 >>> fruits[-2]
    8 'banana'

    3.切片

     1 >>> fruits = ['apple','banana','orange','peal','grape']
     2 >>> fruits[1:4]    #取下标1到下标4之间的数,包括1但不包括4
     3 ['banana', 'orange', 'peal']
     4 >>> fruits[1:-1]   #取下标1至-1之间的数,不包括-1
     5 ['banana', 'orange', 'peal']
     6 >>> fruits[0:3]    #从头开始取,不包括3
     7 ['apple', 'banana', 'orange']
     8 >>> fruits[:3]     #和上句一样
     9 ['apple', 'banana', 'orange']
    10 >>> fruits[3:]     #从下标3到最后,到末尾只能这样取
    11 ['peal', 'grape']
    12 >>> fruits[0::2]   #从头开始,步长为2,即隔一个取一个
    13 ['apple', 'orange', 'grape']
    14 >>> fruits[::2]    #和上句一样
    15 ['apple', 'orange', 'grape']

    4.追加,append()

    1 >>> fruits
    2 ['apple', 'banana', 'orange', 'peal', 'grape']
    3 >>> fruits.append('newpeach')
    4 >>> fruits
    5 ['apple', 'banana', 'orange', 'peal', 'grape', 'newpeach']

    5.插入元素,insert()

    在下标1处插入一个西瓜(watermelon)

    1 ['apple', 'banana', 'orange', 'peal', 'grape', 'newpeach']
    2 >>> fruits.insert(1,'watermelon')
    3 >>> fruits
    4 ['apple', 'watermelon', 'banana', 'orange', 'peal', 'grape', 'newpeach']

    6.修改列表中的元素

    将banana修改为樱桃cherry

    1 >>> fruits
    2 ['apple', 'watermelon', 'banana', 'orange', 'peal', 'grape', 'newpeach']
    3 >>> fruits[2]='cherry'
    4 >>> fruits
    5 ['apple', 'watermelon', 'cherry', 'orange', 'peal', 'grape', 'newpeach']

    7.删除

    pop()在默认删除最后一个元素后,会返回该元素

     1 >>> fruits
     2 ['apple', 'watermelon', 'cherry', 'orange', 'peal', 'grape', 'newpeach']
     3 >>> del fruits[2]    #删除第二个元素
     4 >>> fruits
     5 ['apple', 'watermelon', 'orange', 'peal', 'grape', 'newpeach']
     6 >>> fruits.remove('orange')     #删除指定的元素
     7 >>> fruits
     8 ['apple', 'watermelon', 'peal', 'grape', 'newpeach']
     9 >>> fruits.pop()    #删除最后一个元素
    10 'newpeach'
    11 >>> fruits
    12 ['apple', 'watermelon', 'peal', 'grape']

    8.扩展 extend()

     1 >>> fruits
     2 ['apple', 'watermelon', 'peal', 'grape']
     3 >>> vegetable = ['radish','cabbage','cucumber']
     4 >>> fruits
     5 ['apple', 'watermelon', 'peal', 'grape']
     6 >>> vegetable
     7 ['radish', 'cabbage', 'cucumber']
     8 >>> fruits.extend(vegetable)
     9 >>> fruits
    10 ['apple', 'watermelon', 'peal', 'grape', 'radish', 'cabbage', 'cucumber']

     9.拷贝 copy()

    1 ['apple', 'watermelon', 'peal', 'grape', 'radish', 'cabbage', 'cucumber']
    2 >>> fruits2 = fruits.copy()
    3 >>> fruits2
    4 ['apple', 'watermelon', 'peal', 'grape', 'radish', 'cabbage', 'cucumber']

    10.统计 count()

    1 >>> fruits.count('apple')
    2 1

    11.排序 sort() 和翻转 reverse()

    1 >>> fruits
    2 ['apple', 'watermelon', 'peal', 'grape', 'radish', 'cabbage', 'cucumber']
    3 >>> fruits.sort()
    4 >>> fruits
    5 ['apple', 'cabbage', 'cucumber', 'grape', 'peal', 'radish', 'watermelon']
    6 >>> fruits.reverse()
    7 >>> fruits
    8 ['watermelon', 'radish', 'peal', 'grape', 'cucumber', 'cabbage', 'apple']

    12.获取下标 index()

    1 ['watermelon', 'radish', 'peal', 'grape', 'cucumber', 'cabbage', 'apple']
    2 >>> fruits.index('apple')
    3 6
    # 只返回找到的第一个下标
  • 相关阅读:
    如何实现进程间的通信
    调试手记
    WinCE的一些忠告——UNICODE编码
    一道字符串复制的面试题目
    strcpy和strncpy区别
    关于#include头文件问题
    rs232串口通讯中,读串口与读端口的区别
    IP包过滤(转)
    小数点后截位问题
    一些函数
  • 原文地址:https://www.cnblogs.com/bigberg/p/6397203.html
Copyright © 2011-2022 走看看