python基础--列表

高洛峰
发布: 2017-02-17 11:41:12
原创
1656人浏览过

列表

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

1.定义列表

fruits = ['apple','banana','orange']
登录后复制

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

>>> fruits[0]
'apple'
>>> fruits[2]
'orange'
>>> fruits[-1]
'orange'
>>> fruits[-2]
'banana'
登录后复制

3.切片

立即学习Python免费学习笔记(深入)”;

>>> fruits = ['apple','banana','orange','peal','grape']
>>> fruits[1:4]    #取下标1到下标4之间的数,包括1但不包括4
['banana', 'orange', 'peal']
>>> fruits[1:-1]   #取下标1至-1之间的数,不包括-1
['banana', 'orange', 'peal']
>>> fruits[0:3]    #从头开始取,不包括3
['apple', 'banana', 'orange']
>>> fruits[:3]     #和上句一样
['apple', 'banana', 'orange']
>>> fruits[3:]     #从下标3到最后,到末尾只能这样取
['peal', 'grape']
>>> fruits[0::2]   #从头开始,步长为2,即隔一个取一个
['apple', 'orange', 'grape']
>>> fruits[::2]    #和上句一iy
['apple', 'orange', 'grape']
登录后复制

4.追加,append()

>>> fruits
['apple', 'banana', 'orange', 'peal', 'grape']
>>> fruits.append('newpeach')
>>> fruits
['apple', 'banana', 'orange', 'peal', 'grape', 'newpeach']
登录后复制

5.插入元素,insert()

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

['apple', 'banana', 'orange', 'peal', 'grape', 'newpeach']
>>> fruits.insert(1,'watermelon')
>>> fruits
['apple', 'watermelon', 'banana', 'orange', 'peal', 'grape', 'newpeach']
登录后复制

6.修改列表中的元素

将banana修改为樱桃cherry

表单大师AI
表单大师AI

一款基于自然语言处理技术的智能在线表单创建工具,可以帮助用户快速、高效地生成各类专业表单。

表单大师AI 74
查看详情 表单大师AI
>>> fruits
['apple', 'watermelon', 'banana', 'orange', 'peal', 'grape', 'newpeach']
>>> fruits[2]='cherry'
>>> fruits
['apple', 'watermelon', 'cherry', 'orange', 'peal', 'grape', 'newpeach']
登录后复制

7.删除

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

>>> fruits
['apple', 'watermelon', 'cherry', 'orange', 'peal', 'grape', 'newpeach']
>>> del fruits[2]    #删除第二个元素
>>> fruits
['apple', 'watermelon', 'orange', 'peal', 'grape', 'newpeach']
>>> fruits.remove('orange')     #删除指定的元素
>>> fruits
['apple', 'watermelon', 'peal', 'grape', 'newpeach']
>>> fruits.pop()    #删除最后一个元素
'newpeach'
>>> fruits
['apple', 'watermelon', 'peal', 'grape']
登录后复制

8.扩展 extend()

 >>> fruits
['apple', 'watermelon', 'peal', 'grape']
>>> vegetable = ['radish','cabbage','cucumber']
>>> fruits
['apple', 'watermelon', 'peal', 'grape']
>>> vegetable
['radish', 'cabbage', 'cucumber']
>>> fruits.extend(vegetable)
>>> fruits
['apple', 'watermelon', 'peal', 'grape', 'radish', 'cabbage', 'cucumber']
登录后复制

9.拷贝 copy()

['apple', 'watermelon', 'peal', 'grape', 'radish', 'cabbage', 'cucumber']
>>> fruits2 = fruits.copy()
>>> fruits2
['apple', 'watermelon', 'peal', 'grape', 'radish', 'cabbage', 'cucumber']
登录后复制

10.统计 count()

 >>> fruits.count('apple')
 1
登录后复制

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

>>> fruits
['apple', 'watermelon', 'peal', 'grape', 'radish', 'cabbage', 'cucumber']
>>> fruits.sort()
>>> fruits
['apple', 'cabbage', 'cucumber', 'grape', 'peal', 'radish', 'watermelon']
>>> fruits.reverse()
>>> fruits
['watermelon', 'radish', 'peal', 'grape', 'cucumber', 'cabbage', 'apple']
登录后复制

12.获取下标 index()

['watermelon', 'radish', 'peal', 'grape', 'cucumber', 'cabbage', 'apple']
>>> fruits.index('apple')
6
# 只返回找到的第一个下标
登录后复制

 更多python基础--列表相关文章请关注PHP中文网!

python速学教程(入门到精通)
python速学教程(入门到精通)

python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号