1.1 列表的介绍
列表是python的基础数据类型之⼀一 ,其他编程语⾔言也有类似的数据类型. 比如JS中的熟组, java中的数组等. 它是以[ ]括起来, 每个元素⽤用' , '隔开⽽而且可以存放各种数据类型:
lst = [1, '哈哈', "吼吼", [1,8,0,"百度"], ("我","叫", "元", "组"), "abc", {"我叫":"dict字典"},{"我叫集合","集合"}]
列表相比于字符串串. 不仅可以存放不同的数据类型. ⽽而且可以存放⼤大量量的数据. 32位python可以存放: 536870912个元素, 64位可以存放: 1152921504606846975个元素.而且列表是有序的(按照你保存的顺序),有索引, 可以切片方便取值.
2.2 列表的索引和切片
列表和字符串串一样也拥有索引:
list = ['哈哈','呵呵','嘻嘻','哼哼'] list[0] ="GG" print(list) list[1] = "HH" print(list)
列表的切片:
list = ['哈哈','呵呵','嘻嘻','哼哼'] print(list[0:3]) # ['哈哈', '呵呵', '嘻嘻'] print(list[:3]) # ['哈哈', '呵呵', '嘻嘻'] print(list[1::2]) # ['呵呵', '哼哼'] print(list[2::-1]) # ['嘻嘻', '呵呵', '哈哈'] print(list[-1:-3:-2]) # ['哼哼']
二. 列表的增删改查
1. 增, 注意, list和str是不一样的. lst可以发生改变. 所以直接就在原来的对象上进⾏了操作
list = ['哈哈','呵呵','嘻嘻','哼哼'] print(list) list.append("嘎嘎") print(list) list = [] while True: content = input("请输入你要录入的员工信息,输入Q退出:") if content.upper() =="Q": break list.append(content) print(list) list = ['哈哈','呵呵','嘻嘻','哼哼'] list.insert(1,"嘎嘎") #在1的位置插入嘎嘎,原来元素向后移动一位 print(list)
# 迭代添加
list = ['哈哈','呵呵','嘻嘻','哼哼']
list1 = ["星星","月亮"]
list.extend(list1)
print(list)
2. 删除
pop, remove, clear, del
list = ['哈哈','呵呵','嘻嘻','哼哼'] print(list) deleted = list.pop() # 删除最后一个 print("被删除的", deleted) print(list) list = ['哈哈','呵呵','嘻嘻','哼哼'] deleted = list.pop(1) # 删除1号元素 print("被删除的", deleted) print(list) list = ['哈哈','呵呵','嘻嘻','哼哼'] deleted = list.remove("嘻嘻") # 删除指定元素 print(list) list = ['哈哈','呵呵','嘻嘻','哼哼'] deleted = list.clear() # 清空list print(list) list = ['哈哈','呵呵','嘻嘻','哼哼'] del list[1:3] print(list)
3. 修改
索引切片修改
list = ['哈哈','呵呵','嘻嘻','哼哼'] list[1]="GG" print(list) list = ['哈哈','呵呵','嘻嘻','哼哼'] list[:3:2]=["G","T"] print(list) list = ['哈哈','呵呵','嘻嘻','哼哼'] list[1:3]=["gg"] print(list)
4. 查询, 列表是一个可迭代对象, 所以可以进行for循环
list = ['哈哈','呵呵','嘻嘻','哼哼'] for c in list: print(c)
5、其他操作
list = ['哈哈','呵呵','嘻嘻','哼哼',"哈哈","哈哈"] print(list.count("哈哈")) list = [1,11,22,2] list.sort() print(list) #排序,默认升序 list.sort(reverse=True) # 降序 print(list) list = ['哈哈','呵呵','嘻嘻','哼哼',"哈哈","哈哈"] print(list) list.reverse() print(list) list = ['哈哈','呵呵','嘻嘻','哼哼',"哈哈","哈哈"] print(len(list)) #列表的长度
三. 列表的嵌套
采用降维操作.一层一层的看就好.
list= [1,"xx","zz",["GG",["KK"],"LL"]] print(list[2]) #找到zz print(list[3][1][0]) #找到KK
print(list[1][1]) #找到x
# 将zz拿到 然后首字母大写 再放回去
list= [1,"xx","zz",["GG",["KK"],"LL"]]
s = list[2]
s = s.capitalize()
list[2] = s
print(list)
#把zt变成zx
list= [1,"xx","zt",["GG",["KK"],"LL"]]
list[2]=list[2].replace("t","x")
print(list)
# 把GG换成GT
list= [1,"xx","zt",["GG",["KK"],"LL"]]
list[3][0] = list[3][0].replace("GG","GT")
print(list)
#在KK后面增加PP
list= [1,"xx","zt",["GG",["KK"],"LL"]]
list[3][1].append("PP")
print(list)
四. 元组和元组嵌套
元组: 俗称不可变的列表.又被成为只读列表, 元组也是python的基本数据类型之一, ⽤用小括号括起来, 里面可以放任何数据类型的数据, 查询可以. 循环也可以. 切片也可以. 但就是不能改.
tu = (1, "太白", "李白", "太黑", "怎么黑") print(tu) print(tu[0]) print(tu[2]) print(tu[2:4]) # 切⽚片之后还是元组 # for循环遍历元组 for el in tu : print(el) tu = (1,"哈哈",[],"呵呵") # tu[2] = ["abcd"] #这样改不行 tu[2].append("马化腾") #可以改 tu[2].append("王健林") print(tu)
关于不可变, 注意: 这里元组的不可变的意思是子元素不可变. 而⼦元素内部的子元素是可以变, 这取决于子元素是否是可变对象.
元组中如果只有一个元素. ⼀定要添加一个逗号, 否则就不是元组
tu = (1,) print(type(tu))
元组也有count(), index(), len()等⽅法.
tu = ("a","b","c","d","a") print(tu.count("a")) tu = ("a","b","c","d","a") print(tu.index("b")) tu = ("a","b","c","d","a") print(len(tu))
五. range
range可以帮我们获取到一组数据. 通过for循环能够获取到这些数据.
for num in range(10): print(num) for num in range(1,10,2): print(num) for num in range(10,1,-1): print(num) # 反着来, 和切片一样