1.列表 list[ ]
列表是python中的基础数据类型之一,其他语言中也有类似于列表的数据类型,比如js中叫数组,他是以[ ] 括起来,每个元素以逗号隔开,而且他里面可以存放各种数据类型比如:li = [‘span’,123,True,(1,2,3,’python’),[1,2,3,’小明’,],{‘name’:’span’}]
列表相比于字符串,不仅可以储存不同的数据类型,而且可以储存大量数据,32位python的限制是 536870912 个元素,64位python的限制是 1152921504606846975 个元素。而且列表是有序的,有索引值,可切片,方便取值。
1.1 列表有序,意味着可以用索引、切片取数据,也可以跳着切片。
li = ["span",[18,188,1888],"python","金融","FX-EXchange"] l1 = li[2] l2 = li[1] print(l1) print(l2)
l3 = li[0:3]
print(l3)
>>>python
>>>[18, 188, 1888]
>>>['span', [18, 188, 1888], 'python']
1.2 #增加
list.append() 增加到最后
li = ["span",[18,188,1888],"python","金融","FX-EXchange"] li.append("Math") print(li) >>>['span', [18, 188, 1888], 'python', '金融', 'FX-EXchange', 'Math']
拓展:持续向列表里添加元素,按“Q”退出
while 1: username = input(">>>:") if username.strip().upper() == "Q": break else: li.append(username) print(li)
# 插入 list.insert(索引,插入的值)
#添加 list.extend("iterable") 迭代的添加,将添加的值分成可迭代的元素,迭代得添加(分解到最后的可迭代的元素元素)
li = ["span",[18,188,1888],"python","金融","FX-EXchange"] li.extend("Math") li.extend([1,2,3]) print(li) >>>['span', [18, 188, 1888], 'python', '金融', 'FX-EXchange', 'M', 'a', 't', 'h', 1, 2, 3]
PS:int object is not iterable.
1.3
#删除 list.pop(index) 按索引删除
li = ["span",[18,188,1888],"python","金融","FX-EXchange"] item = li.pop(2)#有返回值,默认是删除最后一个 print(item,li) >>>python ['span', [18, 188, 1888], '金融', 'FX-EXchange']
#删除 list.remove() 按元素删除
li = ["span",[18,188,1888],"python","金融","FX-EXchange"] li.remove("span")#无返回值 print(li) >>>[[18, 188, 1888], 'python', '金融', 'FX-EXchange']
#清空一个列表 list.clear() #清空一个列表,列表仍存在
#删除一个列表 del list
del li print(li) >>>NameError: name 'li' is not defined
li = ["span",[18,188,1888],"python","金融","FX-EXchange"] del li[2:]#切片删除 print(li) >>>['span', [18, 188, 1888]]
1.4 改动列表里面的值,可直接按索引赋值 如:list[index] = “”
还可以切片改:
li = ["span",[18,188,1888],"python","金融","FX-EXchange"] li[0:2] = "我爱"#iterable value print(li) >>>['我', '爱', 'python', '金融', 'FX-EXchange']
li = ["span",[18,188,1888],"python","金融","FX-EXchange"] li[0:2] = [1,2,3,"我","爱"] print(li) >>>[1, 2, 3, '我', '爱', 'python', '金融', 'FX-EXchange']
1.5 查 #可按索引查,也可按切片查 也可以通过for循环查找所有的元素
li = ["span",[18,188,1888],"python","金融","FX-EXchange"] for i in li: print(i) >>> span [18, 188, 1888] python 金融 FX-EXchange
注:公共方法:
列表元素的个数len();元素出现的个数:list.count(value);通过元素找索引 list.index(value)
li = ["span",[18,188,1888],"python","金融","FX-EXchange"] i = len(li) num = li.count("spam") index = li.index("python")#如果查不到会报错 print(i,num,index) >>> 5 1 2
# 排序
list.sort()#正序排序;
list.sort(reverse=True)#倒序排序
list.reverse()#反转
#####补充:列表的嵌套及其他操作
一、找到列表中的某个元,并显示元素的元素
#补充:列表的嵌套 li = ["span",[18,188,1888],"python","金融","FX-EXchange"] print(li[3][0])#找到“金融”的金字,并将其打印 >>>金
#找到列表第一个元素,将其首字母大写,并放回 li = ["span",[18,188,1888],"python","金融","FX-EXchange"] name = li[0].capitalize() li[0] = name print(li[0]) >>>Span
#找到‘金融’二字,将‘融’字替换成“元宝”
li = ["span",[18,188,1888],"python","金融","FX-EXchange"]
#li[3] = "金元宝"
#或者
li[3] = li[3].replace("融","元宝")
print(li)
>>>['span', [18, 188, 1888], 'python', '金元宝', 'FX-EXchange']
****元组被称为只读列表,即数据可以被查询,但不能被修改,所以,字符串的切片操作同样适用于元组。***
# tuple
#元组,只读列表,可查询可切片:儿子不能够孙子能改 tu = ("span",[18,188,1888],["python",123,666],"金融","FX-EXchange") #将1888,改成808 tu[1][2] = 808 print(tu) >>>('span', [18, 188, 808], ['python', 123, 666], '金融', 'FX-EXchange') #在列表后面添加9999 tu[1].append(999) print(tu) >>>('span', [18, 188,
#列表转换成字符串"".join(iterable)
###列表转换成字符串
li = ["span","金融","FX-EXchange"] s = "".join(li) print(s) >>>span金融FX-EXchange name = "span" s = "_".join(name) print(s) >>>s_p_a_n
#打印列表中的每一个元素
li = ["span",[18,188,1888],["python",123,666],"金融","FX-EXchange"] for i in range(len(li)): if type(li[i]) == list: for j in li[i]: print(j,end=" ") else: print(li[i],end=" ") >>>span 18 188 1888 python 123 666 金融 FX-EXchange
###元组里面有一个元素且不加逗号,此元素是什么类型就是什么类型
## tu1 = (1) tu2 = (1,) print(tu1,type(tu1)) print(tu2,type(tu2)) 》》》1 <class 'int'> 》》》(1,) <class 'tuple'>