zoukankan      html  css  js  c++  java
  • Python List列表的操作说明

    Python中List的N种操作,其简单程度令人叹为观止...

    C:Users
    hys>python
    Python 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:19:30) [MSC v.1500 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> listOne
    [1, 3, 5, 7, 9] #1、赋值
    >>> listTwo
    [10, 4, 8, 6]
    >>> listTotal = listOne+listTwo #2、合并
    >>> listTotal
    [1, 3, 5, 7, 9, 10, 4, 8, 6]
    >>> listOne.extend(listTwo) #3、将listTwo并入listOne
    >>> print(listOne)
    [1, 3, 5, 7, 9, 10, 4, 8, 6]
    >>> listTotal.append(2) #4、增加元素
    >>> listTotal.sort() #5、排序
    >>> listTotal
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    >>> listTotal[7] #6、根据序号寻找元素
    8
    >>> listTotal.index(5)#7、根据元素返回索引
    4 >>> del(listTotal[9]) #8、删除元素 >>> listTotal[10]=10 >>> listTotal.append(10) >>> listTotal [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> len(listTotal) #9、返回长度 10 >>> 2 in listTotal #10、是否有某元素 True >>> y=[1]*4 #11、复制元素 >>> y [1, 1, 1, 1] >>>

     续:

    >>> listTotal=[1,2,3,4,5,6,7,8,9,10]
    >>> listTotal[-1] #使用负数索引来表示,导数第n个元素
    10
    >>> listTotal[-2]
    9
    >>> listTotal[3:] #使用index1:index2,来表示从两个索引之间的子集
    [4, 5, 6, 7, 8, 9, 10]
    >>> listTotal[-3:]
    [8, 9, 10]
    >>> mySubList = listTotal[-5:]
    >>> mySubList
    [6, 7, 8, 9, 10]
    >>> listTotal[-8:-3]
    [3, 4, 5, 6, 7]
    >>> listTotal[:-3]
    [1, 2, 3, 4, 5, 6, 7]
    
  • 相关阅读:
    AS400一些有用的命令
    Publish的时候某些需要用到的文件没deploy上去
    DB2一些SQL的用法
    根据PostgreSQL 系统表查出字段描述
    linux memcached 安装
    CentOS下XEN虚拟服务器安装配置
    Apache the requested operation has failed
    PHP配置兼容ZendDebugger和Optimizer
    虚拟机比较
    memcache 运行情况,内存使用
  • 原文地址:https://www.cnblogs.com/rhyswang/p/8142310.html
Copyright © 2011-2022 走看看