1. List (list)
Operations: indexing, slicing, incrementing, multiplying, searching (note: the data items of the list do not need to have the same type, similar to tuple, but the list allows modification of values, but tuple does not)
Flip (List.reverse()), sort (List.sort()), list nesting (List=[list1, list2]; it feels like two one-dimensional arrays form a two-dimensional array?? Maybe), etc.
Indexing: like array access
Slicing: List[2:4] (left closed and right open)
Increase: List.append('a') (add to the end), List.insert(*, 'a') (insert 'a' at position *; move the rest behind),
List.extend('a', 'b', 'c') , List.extend(list2) , List += list2; (adds a list)
删:del List[2]、del List
example:
list1 = ['Google', 'Runoob', 1997, 2000]
list2 = [1, 2, 3, 4, 5 ]
list3 = ["a", "b", "c", "d"]
list1 += list2
print("list1:%s, type:%s"%(list1, type(list1)))
str_x = '-31210'
'''
1.数字在后 list为01234
可将str_x看做下表看做 0,1,2,3,4,5
-6,-5,-4,-3,-2,-1
正数:list[:4] 左闭右开 输出0,1,2,3位置处的值,即‘-312’
负数:list[:-2] 同上 右开 输出 -6,-5,-4,-3位置处的值,即‘-312’
2. :: 情况下,将角标看做定值,在进行反转
list[::-1] 倒序输出 相当于原list变为01213-
list[2::-1] 数字在左侧,原本为左闭(包含index=2处的值)输出到结尾,::-1代表反转原本输出2,3,4,5位置处的值,
现输出2,1,0索引处的值, 即‘13-’
list[3::-1] 同理,本应输出3,4,5,6索引的值,::-1,反转后,输出3,2,1,0索引处的值,即‘213-’
list[:2:-1] 原本输出0,1索引处的值,不包含2,::-1反转后,输出5,4,3索引处的值,不包含索引2的值,即‘012’
'''
# str_x[:-2]
# str_x[::-1]
# str_x[2::-1]
str_x[:2:-1]
# str_x[:1:-1]
2. Tuple
Tuples are also known as read-only lists, which are not allowed to be modified, and the definitions of the two are different.
The operation is the same as the list.
tup1 = ('Google', 'Runoob', 1997, 2000)
tup2 = (1, 2, 3, 4, 5 )
tup3 = "a", "b", "c", "d" # No need for parentheses
(Note: When the tuple contains only one element, a comma needs to be added after the element; example: tup1 = (1,))
3. Dictionary (dict)
A dictionary is a mutable container model that can store objects of any type. (key-value pair, and the key is unique and immutable, so the list cannot be used as the key, and the value after the same key overrides the previous value; when accessing, the value is accessed through the key)
example:
dict = {'Alice': '2341', 'Beth': 9102, 'Cecil': '3258'}
Addition and modification: dict['A'] = 1; dict['Alice']='11'
delete:
del dict['Name'] # delete key 'Name'
dict.clear() # clear the dictionary
del dict # delete dictionary
4. Collection (set)
A set is an unordered sequence of elements that do not repeat
set1 = {'A', 'B', 'C', 'B'} #If repeated, deduplicate
'C' in set1 #judging whether the element is in the set returns True, otherwise False
Operations and built-in functions: https://www.runoob.com/python3/python3-set.html