章节
Python 介绍 https://www.qikegu.com/docs/2744
Python 开发环境搭建 https://www.qikegu.com/docs/2750
Python 语法 https://www.qikegu.com/docs/2752
Python 变量 https://www.qikegu.com/docs/2754
Python 数值类型 https://www.qikegu.com/docs/2757
Python 类型转换 https://www.qikegu.com/docs/2759
Python 字符串 (String) https://www.qikegu.com/docs/2761
Python 运算符 https://www.qikegu.com/docs/2763
Python 列表 (list) https://www.qikegu.com/docs/2766
Python 元组 (Tuple) https://www.qikegu.com/docs/2769
Python 集合 (Set) https://www.qikegu.com/docs/2774
Python 字典 (Dictionary) https://www.qikegu.com/docs/2776
Python If ... Else https://www.qikegu.com/docs/2779
Python While 循环 https://www.qikegu.com/docs/2781
Python For 循环 https://www.qikegu.com/docs/2783
Python 函数 https://www.qikegu.com/docs/2785
Python Lambda https://www.qikegu.com/docs/2787
Python 类与对象 https://www.qikegu.com/docs/2790
Python 继承 https://www.qikegu.com/docs/2793
Python 迭代器 (Iterator) https://www.qikegu.com/docs/2801
Python 模块 https://www.qikegu.com/docs/2803
Python 日期 (Datetime) https://www.qikegu.com/docs/2806
Python JSON https://www.qikegu.com/docs/2808
Python 正则表达式 (RegEx) https://www.qikegu.com/docs/2810
Python PIP 包管理器 https://www.qikegu.com/docs/2813
Python 异常处理 (Try...Except) https://www.qikegu.com/docs/2815
Python 打开文件 (File Open) https://www.qikegu.com/docs/2817
Python 读文件 https://www.qikegu.com/docs/2819
Python 写文件 https://www.qikegu.com/docs/2821
Python 删除文件与文件夹 https://www.qikegu.com/docs/2823
Python 集合 (数组)
Python 编程语言中有四种集合数据类型:
List 是一个有序且可修改的集合. 允许有重复的成员.
Tuple 是一个有序且不可更改的集合. 允许有重复的成员.
Set 是一个无序且无索引的集合. 没有重复的成员.
Dictionary 是一个无序的, 可修改的, 有索引的集合. 没有重复的成员.
深入理解类型特性有助于我们选择正确的集合数据类型.
list / 列表
list / 列表是一个有序且可更改的集合. 在 Python 中, 列表是用方括号包裹的.
Python 数组 / array
Python 没有内置的数组类型, 列表的功能类似数组, 可以把列表当作数组用.
示例
创建列表:
- thislist = ["自行车", "汽车", "高铁"]
- print(thislist)
访问列表项
你可以通过索引访问列表项:
示例
打印列表第二项:
- thislist = ["自行车", "汽车", "高铁"]
- print(thislist[1])
修改列表项的值
若要修改某个列表项的值, 使用索引:
示例
更改第二项:
- thislist = ["自行车", "汽车", "高铁"]
- thislist[1] = "飞机"
- print(thislist)
遍历列表
你可以使用 for 循环遍历列表项:
示例
逐个打印列表中的所有项目:
- thislist = ["自行车", "汽车", "高铁"]
- for x in thislist:
- print(x)
检查列表项是否存在
若要确定列表中是否存在某项, 请使用 in 关键字:
示例
逐个打印列表中的所有项目:
- thislist = ["自行车", "汽车", "高铁"]
- if "自行车" in thislist:
- print("交通工具中包含自行车")
列表长度
要确定列表中有多少项, 可以使用 len() 方法:
示例
打印列表长度:
- thislist = ["自行车", "汽车", "高铁"]
- print(len(thislist))
添加项目
若要向列表末尾添加项目, 可使用 append() 方法:
示例
添加项目:
- thislist = ["自行车", "汽车", "高铁"]
- thislist.append("飞机")
- print(thislist)
若要在指定索引处添加项目, 使用 insert() 方法:
示例
插入项目:
- thislist = ["自行车", "汽车", "高铁"]
- thislist.insert(1, "飞机")
- print(thislist)
删除项目
从列表中删除项目有几种方法:
示例
remove() 删除指定项目:
- thislist = ["自行车", "汽车", "高铁"]
- thislist.remove("汽车")
- print(thislist)
示例
pop() 方法删除指定的索引 (如果没有指定索引, 则删除最后一项):
- thislist = ["自行车", "汽车", "高铁"]
- thislist.pop()
- print(thislist)
示例
del 关键字删除指定的索引:
- thislist = ["自行车", "汽车", "高铁"]
- del thislist[0]
- print(thislist)
示例
del 关键字也可以完全删除列表:
- thislist = ["自行车", "汽车", "高铁"]
- del thislist
示例
clear() 方法清空列表:
- thislist = ["自行车", "汽车", "高铁"]
- thislist.clear()
- print(thislist)
复制一个列表
不能简单地通过输入 list2 = list1 来复制列表, list2 只引用了 list1, 并且 list1 中所做的更改也将体现在 list2 中.
复制有多种方法, 其中一种方法是使用内置的 List 方法 copy().
示例
使用 copy() 方法复制列表:
- thislist = ["自行车", "汽车", "高铁"]
- mylist = thislist.copy()
- print(mylist)
复制的另一种方法是使用内置的函数 list().
示例
使用 list() 方法复制一个列表:
- thislist = ["自行车", "汽车", "高铁"]
- mylist = list(thislist)
- print(mylist)
list() 构造函数
可以使用 list() 构造函数创建一个新列表.
示例
使用 list() 构造函数创建一个列表:
- thislist = list(("自行车", "汽车", "高铁")) # 注意双圆括号
- print(thislist)
list 方法
Python 有一组常用的列表内置方法.
方法 | 描述 |
---|---|
append() | 在列表末尾添加一个元素 |
clear() | 从列表中删除所有元素 |
copy() | 返回列表的副本 |
count() | 返回具有指定值的元素数量 |
extend() | 将列表 (或任何可迭代的) 的元素添加到当前列表的末尾 |
index() | 返回具有指定值的第一个元素的索引 |
insert() | 在指定位置添加元素 |
pop() | 移除指定位置的元素 |
remove() | 移除具有指定值的项 |
reverse() | 反转列表的顺序 |
sort() | 排序的列表 |
来源: http://www.bubuko.com/infodetail-3158837.html