List is a collection which is ordered and changeable. Allows duplicate members. In Python lists are written with square brackets.
https://www.w3schools.com/python/python_lists.asp
列表是一个有序和可更改的集合, 允许存在重复的成员, 在python里用中括号[]来表示
thislist = ["apple", "banana", "cherry"]
print(thislist)
-> ['apple', 'banana', 'cherry']
thislist = ["apple", "banana", "cherry"]
thislist.append("orange")
print(thislist)
->['apple', 'banana', 'cherry', 'orange']
thislist = ["apple", "banana", "cherry"]
thislist.insert(1, "orange")
print(thislist)
->['apple', 'orange', 'banana', 'cherry']
thislist = ["apple", "banana", "cherry"]
thislist.remove("banana")
print(thislist)
->["apple", 'cherry']
thislist = ["apple", "banana", "cherry"]
thislist.pop()
print(thislist)
->["apple", 'banana']
thislist = ["apple", "banana", "cherry"]
del thislist[0]
print(thislist)
-> ["banana", "cherry"]
thislist = ["apple", "banana", "cherry"]
del thislist
print(thislist)
-> NameError: name 'thislist' is not defined
thislist = ["apple", "banana", "cherry"]
thislist.clear()
print(thislist)
->[]
>>>a = [5,7,6,3,4,1,2]
>>> b = sorted(a) # 保留原列表
>>> a
[5, 7, 6, 3, 4, 1, 2]
>>> b
[1, 2, 3, 4, 5, 6, 7]
>>> L=[('b',2),('a',1),('c',3),('d',4)]
[('a', 1), ('b', 2), ('c', 3), ('d', 4)]
>>> sorted(L, key=lambda x:x[1]) # 利用key
[('a', 1), ('b', 2), ('c', 3), ('d', 4)]
>>> students = [('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]
>>> sorted(students, key=lambda s: s[2]) # 按年龄排序
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
>>> sorted(students, key=lambda s: s[2], reverse=True) # 按降序
[('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]
L = [1, 2, 3]
for i in reversed(L):
print(i)
# 3
# 2
# 1
7.list.reversed()
L = [3,5,2,1,4]
L.reverse()
print(L)
# [5,4,3,2,1]
8, list.append(obj) --> 追加对象到list列表里
9. list,count(obj) -->统计某个元素在列表中出现的个数
10. list.extend(list) --> 追加一个可迭代对象到list (比如列表里加列表)
L = [1,2,3]
A = [4,5,6]
L.extend(A)
print(L)
# [1,2,3,4,5,6]
# !!!! 该方法返回值为None,修改的是原列表
b = 'my..name..is..bob'
print(b.split())
# ['my..name..is..bob']
print(b.split(".."))
# ['my', 'name', 'is', 'bob']
print(b.split("..",0))
# ['my..name..is..bob']
print(b.split("..",1))
# ['my', 'name..is..bob']
print(b.split("..",2))
# ['my', 'name', 'is..bob']
print(b.split("..",-1))
# ['my', 'name', 'is', 'bob']
可以看出 b.split("..",-1)等价于b.split("..")
thistuple = tuple(("apple", "banana", "cherry")) # note the double round-brackets
print(thistuple)
->("apple", "banana", "cherry")
‘%’ 字符,用于标记转换符的起始。
映射键(可选),由加圆括号的字符序列组成 (例如 (somename))。
转换旗标(可选),用于影响某些转换类型的结果。
最小字段宽度(可选)。 如果指定为 ‘*’ (星号),则实际宽度会从 values 元组的下一元素中读取,要转换的对象则为最小字段宽度和可选的精度之后的元素。
精度(可选),以在 ‘.’ (点号) 之后加精度值的形式给出。 如果指定为 ‘*’ (星号),则实际精度会从 values 元组的下一元素中读取,要转换的对象则为精度之后的元素。
长度修饰符(可选)。
转换类型。