标签:move ffffff 表示 sort 操作 copy 字符串 iter 指定
li = [1, 12, 9, "age", ["大白", "小黑"], "alex"]
1. 中括号括起来
2. 逗号分割每个元素
3. 列表中的元素可以是数字、字符串、列表、布尔值等
4. 索引取值
5. 切片取值---->得到的还是列表
6. for循环
7. while 循环
8. 支持 in 操作
列表在内存中是以链表形式存储,可以被修改
1.修改
li = [1, 12, 9, "alex", ["大白", "小黑"]] li[1] = 120 print(li)
li = [1, 12, 9, "alex", ["大白", "小黑"]] li[1: 3] = [120, 90] print(li)
2.删除
li = [1, 12, 9, "alex", ["大白", "小黑"]] del li[1] print(li)
li = [1, 12, 9, "alex", ["大白", "小黑"]] del li[0:3 ] print(li)
3.操作
li = [1, 12, 9, "alex", ["大白", [7, 19]]] v = li[4][1][0] print(v
s = "dabai" new_li = list(s) print(new_li)
for i in 123: print(i)
数字(int)不能进行for循环,不能用list 变为列表,因为字符串转换为列表,内部是进行了 for 循环。
1.既有数字又有字符串时,自己写for循环
li = [11,22,33,"123", "alex"] s = "" for i in li: s = s + str(i) print(s)
2. 如果列表里只有字符串时,用字符串的join方法
li = ["123", "alex"] v = "".join(li) print(v)
li = [11, 22, 33, 44]
v = li.append(5)
print(li)
print(v)
li = [11, 22, 33, 44] li.append([1234,2323]) print(li)
li = [11, 22, 33, 44] li.clear() print(li)
li = [11, 22, 33, 44] v = li.copy() print(v)
li = [11, 22, 33, 22, 44] v = li.count(22) print(v)
li = [11, 22, 33, 22, 44] li.extend(["大白", "521"]) print(li)
li = [11, 22, 33, 22, 44] li.extend("大白") print(li)
li = [11, 22, 33, 22, 44] v= li.index(22) print(v)
li = [11, 22, 33, 22, 44] li.insert(0, 99) print(li)
li = [11, 22, 33, 22, 44] v = li.pop() print(li) print(v)
li = [11, 22, 33, 22, 44] v = li.pop(1) print(li) print(v)
li = [11, 22, 33, 22, 44] li.remove(22) print(li)
li = [11, 22, 33, 22, 44] li.reverse() print(li)
li = [11, 22, 33, 22, 44] li.sort() print(li)
li = [11, 22, 33, 22, 44] li.sort(reverse = True) print(li)
标签:move ffffff 表示 sort 操作 copy 字符串 iter 指定
原文地址:https://www.cnblogs.com/dabai123/p/10960419.html