zoukankan      html  css  js  c++  java
  • 【Python-2.7】list类型

    list是Python中的一种数据类型,也就是"列表"。在Python中我们可以对list类型进行插入,删除,修改等操作。

    ##新建list类型
    >>> ball = ['volleyball','basketball','football','baseball']
    
    ##可以直接打印出list内容
    >>> ball
    ['volleyball', 'basketball', 'football', 'baseball']
    
    ##也可以使用下标列出,注意下标是从0开始的,负数表示从后往前数
    >>> ball[0]
    'volleyball'
    >>> ball[1]
    'basketball'
    >>> ball[2]
    'football'
    
    ##使用append函数,在list最后追加内容
    >>> ball.append('ping-pong')
    >>> ball
    ['volleyball', 'basketball', 'football', 'baseball', 'ping-pong']
    
    ##选择位置插入,比如在'volleyball'后面插入'badminton'
    >>> ball.insert(1,'badminton')
    >>> ball
    ['volleyball', 'badminton', 'basketball', 'football', 'baseball', 'ping-pong']
    
    ##替换list中的某一个元素,比如把'badminton'替换成'bowling'
    >>> ball[1]='bowling'
    >>> ball
    ['volleyball', 'bowling', 'basketball', 'football', 'baseball', 'ping-pong']
    
    ##使用pop()删除元素,比如删除最后的'ping-pong',删除'bowling'
    >>> ball.pop()
    'ping-pong'
    >>> ball
    ['volleyball', 'bowling', 'basketball', 'football', 'baseball']
    >>> ball.pop(1)
    'bowling'
    >>> ball
    ['volleyball', 'basketball', 'football', 'baseball']
    
    ##使用len()查询list中元素个数
    >>> ball
    ['volleyball', 'basketball', 'football', 'baseball']
    >>> len(ball)
    4
  • 相关阅读:
    Property [*****] not found on type [com.erp.pojo.ErpSupplier]
    python 杀死线程的两种方式
    docker的学习之认识docker(一)
    Ubuntu下 MySQL不支持中文解决办法
    Django项目创建
    git介绍
    python 实现链表的逆置
    python 实现链表的增删改查(双向链表)
    python----super
    python面向对象
  • 原文地址:https://www.cnblogs.com/NextAction/p/7492199.html
Copyright © 2011-2022 走看看