1. 创建列表
1.1 创建空列表
emptyList1 = list()
emptyList2 = []
1.2 创建有初始值的列表
intList = [1, 2, 3, 4, 5]
strList = ['a', 'b', 'c']
tupleList = [(1, 2), (2, 3), (3, 3)]
dictList = [{'you': 'perfect'}, {'saxon': 4.0, 'general': 3.99}]
setList = [{1, 2}, {'a', 'b'}, {'b', 3}]
# 可以混合不同类型的数据
mixList = [1, [2, 3], {'saxon': 'perfect'}, (4.0, 4.0), {'Saxon', 'test'}]
2. 列表索引 " 中括号 [] "
print(intList[0]) # 1
print(strList[0]) # a
print(tupleList[0]) # (1, 2)
print(dictList[0]) # {'you': 'perfect'}
print(setList[0]) # {1, 2}
print(mixList[0]) # 1
3. 切片
' 3.1 打印 intList 中的所有项 '
print(intList)
print(intList[:])
' 3.2 打印 intList 中的 2~5 项 '
' method 1:1表示第2项,因为列表是从第0项开始的;切片的右侧index不会到达,所以最多到达intList[5]即第5项 '
print(intList[1:6])
' method 2:因为5项是最后一项,所以也可以表示为打印第 2 项后面所有的项 '
print(intList[1:])
NB: 切片之后的结果仍然是 list
4. 列表脚本操作符
' 4.1 列表组合'
print([1, 2] + [3, 4]) # [1, 2, 3, 4]
' 4.2 列表元素重复'
repeatSaxon1 = ['saxon'] * 3
print(repeatSaxon) # ['saxon', 'saxon', 'saxon']
repeatSaxon2 = ['perfect', 'saxon' * 3]
print(repeatSaxon2) # ['perfect', 'saxonsaxonsaxon']
' 4.3 判断数据是否在列表中 '
print('perfect' in repeatSaxon2) # True
5.简单的列表函数
easyList = [1, 2, 3, 4]
''' 5.1 len()
@input: list
@return: length of list
'''
print(len(easyList)) # 4
''' 5.2 max()
@input: list<integer>
@return: largest element of list
'''
print(max(easyList)) # 4
''' 5.3 min()
@input: list<integer>
@return: smallest element of list
'''
print(min(easyList)) # 1
6. list method
6.1 list.append(“new element”) 在 list 尾部增添新元素
@input: element
@return: None
appendList = [1, 2, 3, 4]
appendList.append(5)
print(methodList) # [1, 2, 3, 4, 5]
6.2 list.count(“element”) 返回 list 中,element出现的次数
@input: element
@return: element在list中的数目
countList = [1, 1, 2, 3]
print(countList.count(1)) # 2
6.3 list1.extend(list2) 在结尾合并另一个list2的元素
@input: list
@return: None
iniList = [1, 2, 3, 4]
extendList = [5, 6, 7]
iniList.extend(extendList)
print(iniList) # [1, 2, 3, 4, 5, 6, 7]
NB: 一般改变了自身的 method 都没有返回值
6.4 list.index(element) 返回第一个匹配项的索引
@input: element
@return: element 第一个位置的index
indexList = [1, 2, 3, 4]
print(indexList.index(1)) # 0
6.5 list.insert(index, element) 在固定index位置插入元素element
@input: index: 位置; element: 要插入的元素
@return: None
insertList = [1, 2, 3, 4]
insertList.insert(1, 'saxon')
print(insertList) # [1, 'saxon', 2, 3, 4]
6.6 list.pop(index) 移除列表中 index 位置的元素,默认index = -1
达到相同效果的还有 del
del[popList[1]]
@input: index: 删除元素的位置
@return: 删除的 Element
popList = ['a', 'b', 'c', 'd', 'e']
popElement1 = popList.pop()
print(popElement1) # e
print(popList) # ['a', 'b', 'c', 'd']
popElement2 = popList.pop(1)
print(popElement2) # b
print(popList) # ['a', 'c', 'd']
6.7 list.remove(element) 移除列表中第一个 element
@input: element: 要删除的元素
@return: None
removeList = [1, 2, 3, 4, 5, 3]
removeList.remove(3)
print(removeList) # [1, 2, 4, 5, 3]
6.8 list.reverse() 反转列表
@input: None
@return: None
reverseList = [1, 2, 'a', 'b', (1, 2)]
reverseList.reverse()
print(reverseList) # [(1, 2), 'b', 'a', 2, 1]
6.9 list.sort() 排序(仅用于纯数值)
@input: None
@return None
sortList = [2, 1, 3, 4]
sortList.sort()
print(sortList) # [1, 2, 3, 4]
sortList.sort(reverse = True)
print(sortList) # [4, 3, 2, 1]
Note: if we use sorted(list), it will not change the list, only for show
sortList2 = [2, 1, 3, 4]
print(sorted(sortList2)) # [1, 2, 3, 4]
print(sortList2) # [2, 1, 3, 4]
6.10 list.clear() 清空列表
clearList = [1, 2, 3, 4]
clearList.clear()
print(clearList) # []
6.11 del
list 范围删除元素 或 直接删除列表
deleteList = [1, 2, 3, 4]
' 范围删除列表 '
del deleteList[1:3]
print(deleteList) # [1, 4]
' 直接删除列表 '
del deleteList
# deleteList 在内存中被删除了
6.12 list.copy() 获取 list 的副本(不在同一个内存地址)
@input: empty
@return: list
orgList = [1, 2, 3, 4]
copyList = orgList.copy()
print(copyList) # [1, 2, 3, 4]
print(id(orgList)) # 2150292580672
print(id(copyList)) # 2150292580736
7. Transform between string and List
7.1 String --> List
7.1.1 list1 = list(string)
@input: string
@return: a list contains each element in string
str1 = 'good morning Saxon!'
list1 = list(str1)
print(list1)
# ['g', 'o', 'o', 'd', ' ', 'm' ... 'o', 'n', '!']
7.1.2 list2 = string.split(’.’)
string = 'hello.saxon'
list2 = string.split('.')
print(list2) # ['hello', 'saxon']
7.2 List --> String
@input: List
@return: a string seperated by 'seperator' and contains all the element in list
str1 = 'good morning Saxon!'
list1 = list(str1)
print(list1)
# ['g', 'o', 'o', 'd', ' ', 'm' ... 'o', 'n', '!']
str2 = ''.join(list1)
print(str2)
# 'good morning Saxon!'
str3 = '~'.join(list1)
print(str3)
'注意这里最后感叹号那里没有 ~'
# 'g~o~o~d~ ~m~o~n~i~n~g~ ~S~a~x~o~n~!'
8. Note:
8.1 References
'Refereces'
list1 = [1, 2, 3]
list2 = [4, 5, 6]
mylist = [list1, list2]
print(mylist) # [[1, 2, 3], [4, 5, 6]]
list1[1] = 'hello'
print(mylist) # [[1, 'hello', 3], [4, 5, 6]]
'Concatenation'
list_a = [1, 2, 3]
list_b = [4, 5, 6]
list_c = list_a + list_b
print(list_c) # [1, 2, 3, 4, 5, 6]
list_a[1] = 'hello'
print(list_c) # [1, 2, 3, 4, 5, 6] not changed!!!