列表


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

1.定义列表

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

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

 >>> fruits[0]
'apple'
>>> fruits[2]
'orange'
>>> fruits[-1]
'orange'
>>> fruits[-2]
'banana'

3.切片

 >>> 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] #和上句一样
['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

 >>> 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基础1--列表的更多相关文章

  1. Python基础数据类型-列表(list)和元组(tuple)和集合(set)

    Python基础数据类型-列表(list)和元组(tuple)和集合(set) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客使用的是Python3.6版本,以及以后分享的 ...

  2. python基础之列表list元组tuple

    作者:tongqingliu 转载请注明出处:http://www.cnblogs.com/liutongqing/p/7041763.html python基础之列表list元组tuple 列表li ...

  3. python基础数据类型--列表(list)

    python基础数据类型--列表(list) 列表是我们在后面经常用到的数据类型之一,通过列表可以对数据类型进行增.删.改.查等操作 一列表的增.删.改.查 1增: 1.1增加到最后   append ...

  4. Day2 - Python基础2 列表、字典、集合

    Python之路,Day2 - Python基础2   本节内容 列表.元组操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码 1. 列表.元组操作 列表是我们最以后最常用的数据类型之一, ...

  5. python基础4 列表和元组

    一. 列表列表:python基础数据类型之一:其他语言中也有列表的概念,js 数组,可索引,可切片,可加步长li = ['hello', 100, True, [1, 2, 3], {'name':' ...

  6. python基础之列表、字典、元祖等 (二)

    一.作用域 if 1==1: name = 'weibinf' print name 下面的结论对吗? 外层变量,可以被内层变量使用 内层变量,无法被外层变量使用 二.三元运算 result = 值1 ...

  7. python基础之列表讲解

    列表是最常用的Python数据类型,它可以作为一个方括号内的逗号分隔值出现. 列表的数据项不需要具有相同的类型 如下图所示,创建一个列表,只要把逗号分隔的不同的数据项使用方括号括起来即可.(接下来的演 ...

  8. Python基础入门-列表解析式

    今天我们使用Python中的列表解析式来实现一些简单功能.好了关于列表解析式是什么?我的理解是它可以根据已有列表,高效创建新列表的方式.列表解析是Python迭代机制的一种应用,它常用于实现创建新的列 ...

  9. Python基础_列表 list

    列表是Python的一种基础数据类型,可以进行的操作包括索引,切片,加,乘,检查成员 列表定义: list(列表.数组) eg:stus=['lisi','jion','peter'] #下标:即角标 ...

  10. python基础类型—列表

    列表 列表是python中的基础数据类型之一,其他语言中也有类似于列表的数据类型,比如js中叫数组,他是以[]括起来,每个元素以逗号隔开,而且他里面可以存放各种数据类型比如: li = [‘alex’ ...

随机推荐

  1. 浏览器差异bug汇总(js篇)

    获取滚动条高度 var scrollTop = document.body.scrollTop || document.documentElement.scrollTop; safari浏览器时间函数 ...

  2. 2017秋-软件工程第十二次作业(一)-PSP总结

    [回顾]:回顾开学时的博客并回答相关问题 1.回想一下你曾经对计算机专业的畅想当初你是如何做出选择计算机专业的决定的?经过一个学期,你的看法改变了么,为什么?答:当初的决定是以前的事情,没有改变.经历 ...

  3. No.1000_第五次团队会议

    光辉的一夜 今夜注定是不平凡的一夜.是崔强同学伟大的一夜. 昨天因为实验室项目,我刚上完编译课就被学院叫走去做项目,当时我就很无奈,因为说好了要和崔强一起实现下午的前端,他写界面我写底层逻辑,这样我们 ...

  4. 3、昨天的BUG

    基本功能实现了,但是有一些小问题,修改昨天余留的BUG

  5. Internet History, Technology and Security (Week5.2)

    Week5 Now, I want to make it real clear that, when I give you a 15 minute video of an amazing invent ...

  6. Week2-作业一——《构建之法》三章精读之想

    Week2-作业一——精读<构建之法> 前言 其实我本人是不经常看书的,电子书倒是看了不少,实体书真的不经常看,但是为了这次作业的需求,我还是选择静下心来阅读一下这本<构建之法> ...

  7. CSS和JS引用图片(资源)的路径问题

    做项目时遇到了这个问题,特此写个笔记记一下

  8. Vue.js——60分钟browserify项目模板快速入门

    概述 在之前的一系列vue.js文章,我们都是用传统模式引用vue.js以及其他的js文件的,这在开发时会产生一些问题. 首先,这限定了我们的开发模式是基于页面的,而不是基于组件的,组件的所有代码都直 ...

  9. 网页正文提取,降噪的实现(readability/Document)

    安装: pip install readability-lxml 使用: # encoding:utf-8import html2textimport requestsimport refrom re ...

  10. (转)Python中如何理解if __name__ == '__main__'

    摘要 通俗的理解 __name__ == '__main__' :假如你叫李凯.py,在朋友眼中,你是李凯( __name__ == '李凯' ):在你自己眼中,你是你自己( __name__ == ...