• 【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
  • 相关阅读:
    [转贴]USB簡介
    [网游计划第六、七天]压力好大,坚持很难
    [备忘] 字符串倒序函数strrev
    我的网游计划:ACM 30天 60题
    [网游计划第一天]:不怎么顺利的开始
    程序员必须要有的自信
    转:squid做反向代理时,要注意的安全设置
    Linux DHCP Server 配置给FIT AP 使用的option
    jdk配置
    nod32升级
  • 原文地址:https://www.cnblogs.com/NextAction/p/7492199.html
Copyright © 2020-2023  润新知