python 基础数据类型之list
python 基础数据类型之list:
1、列表的创建
list1 = ['hello', 'world', 1997, 2000] list2 = [1, 2, 3, 4, 5 ] list3 = ["a", "b", "c", "d"] list4 = list() #创建空列表 list5 = [] #创建空列表
2、访问列表中的值
列表的数据访问需要使用索引序号。 list1 = ['hello', 'world', 19, 20]
list2 = [1, 2, 3, 4, 5 ] print "list1[0]: ", list1[0] print "list2[1:5]: ", list2[1:5]
输出结果:
list1[0]: hello
list2[1:5]: [2, 3, 4, 5]
3、数值更新
列表内容的更新可以直接使用索引序号,进行内容的更新,也可以使用append方法。
list1 = ['hello', 'world', 19, 20] print list1 list1[0] = "HELLO" print list1
运行结果: ['hello', 'world', 19, 20] ['HELLO', 'world', 19, 20]
4、列表元素删除
列表元素的删除使用del语句,也可以使用remove方法。
list1 = ['hello', 'world', 19, 20] print list1 del list1[2] print list1
运行结果: ['hello', 'world', 19, 20] ['hello', 'world', 20]
5、Python列表脚本操作符
列表对 + 和 * 的操作符与字符串相似。+ 号用于组合列表,* 号用于重复列表。
list1 = ['hello', 'world', 19, 20] print list1 print list1 + list1 print list1 * 3
运行结果: ['hello', 'world', 19, 20] ['hello', 'world', 19, 20, 'hello', 'world', 19, 20] ['hello', 'world', 19, 20, 'hello', 'world', 19, 20, 'hello', 'world', 19, 20]
6、列表常用的方法
list.append(obj) #在列表末尾添加新的对象

list1 = ['hello', 'world', 100, 200] list1.append(300) print list1

['hello', 'world', 100, 200, 300]
list.count(obj) #统计某个元素在列表中出现的次数

list1 = ['hello', 'world', 100, 200, "hello"] ret1 = list1.count("hello") ret2 = list1.count(100) print ret1, ret2

2 1
list.extend(seq) #在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)

list1 = ['hello', 'world', ] print list1 list2 = [100, 200, "hello"] list1.extend(list2) print list1

['hello', 'world'] ['hello', 'world', 100, 200, 'hello']
list.index(obj) #从列表中找出某个值第一个匹配项的索引位置

list1 = ['hello', 'world', 100, 200, "hello"] print (list1.index("world")) print (list1.index("hello")) #第一个匹配的位置 print (list1.index(100)) print (list1.index("100")) # 找不到报错

Traceback (most recent call last): 1 File "C:/Users/Administrator/PycharmProject/s1/fileio.py", line 9, in <module> 0 print (list1.index("100")) # 找不到报错 2 ValueError: '100' is not in list
list.insert(index, obj) #将对象插入列表

list1 = ['hello', 'world', ] list2 = [100, 200, "hello", ] list1.insert(0 , "World") list1.insert(0 , list2) # 整个列表作为元素添加到原列表中 print list1

[[100, 200, 'hello'], 'World', 'hello', 'world']
list.pop(obj=list[-1]) #移除列表中的一个元素(默认最后一个元素),并且返回该元素的值

list1 = ['hello', 'world', 100, 200, "hello", ] print list1 list1.pop(0) # 移除索引为0的 print list1 list1.pop() # 默认从最后一个开始移除 print list1

['hello', 'world', 100, 200, 'hello'] ['world', 100, 200, 'hello'] ['world', 100, 200]
list.remove(obj) #移除列表中某个值的第一个匹配项

list1 = ['hello', 'world', 100, 200, "hello", ] print list1 list1.remove("hello") # 删除第一个匹配到的元素 print list1

['hello', 'world', 100, 200, 'hello'] ['world', 100, 200, 'hello']
list.reverse() #反向列表中元素

list1 = ['hello', 'world', 100, 200, "hello", ] list1.reverse() print list1

['hello', 200, 100, 'world', 'hello']
list.sort([func]) #对原列表进行排序 reverse=True 反序

list1 = ['hello', 'world', 100, 200, "hello", ] list2 = [1, 44, 56, 68, 34, 2, 34, 68, 1, 3, 4] list1.sort() list2.sort() print list1 print list2

[100, 200, 'hello', 'hello', 'world'] [1, 1, 2, 3, 4, 34, 34, 44, 56, 68, 68]
7、列表相关的内置函数
cmp(list1, list2) #比较两个列表的元素
len(list) #列表元素个数
max(list) #返回列表元素最大值
min(list) #返回列表元素最小值
list(seq) #将元组转换为列表
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步