1. List
1.1 Format
[Data 1, Data 2, Data 3...] The
list is a variable data type
1.2 Operation
1.2.1 Search
list1 = ["Tom","Jerry","Jack"]
for i in range(3) :
print(list1[i],end=" ")
index(): returns the index of the specified data location, if it does not exist, an error will be reported.
Syntax:
list sequence.index (data, start position index, end position index)
count(): Count the number of times the specified data appears in the current list
len(): Access list length, that is, the number of data in the list.
Syntax:
len (list sequence)
print(len(list1))
Judge whether it exists:
in: Judge whether the specified data is in a certain list sequence, if it is returning true, otherwise it returns false
1.2 Add
append(): append data to the end of the list
Syntax:
list sequence.appen(data)
insert(): Add new data at the specified location.
Syntax:
list sequence. insert ( position subscript, data)
exten(): append data to the end of the list. If the data is a sequence, add the data of this sequence to the list one by one.
Syntax:
list sequence.extend(data)
list1 = ["Tom","Jerry","Jack"]
list1.extend("Ray")
print(list1) # ['Tom', 'Jerry', 'Jack', 'R', 'a', 'y']
list1.extend(["Ray","Paul","James"])
print(list1) # ['Tom', 'Jerry', 'Jack', 'R', 'a', 'y', 'Ray', 'Paul', 'James']
1.3 Delete the
del
syntax:
del target
Delete list:
del list name
Delete specified data:
del list name [subscript]
pop(): Delete the data of the specified subscript (the default is the last one), and return to the modified data.
Syntax:
list sequence.pop(subscript)
list1 = ["Tom","Jerry","Jack"]
name = list1.pop()
print(name) # Jack
remove(): Remove the first match of a certain data in the list.
Syntax:
list sequence.remove(data)
clear(): Clear the list, leaving an empty list at the end
1.4 Modify
rever(): reverse list
syntax:
list sequence.reverse()
sort(): sorting
syntax:
list sequence.sort(key=None,reverse=False)
Note: reverse means sorting rules, reverse=True descending order, reverse=False ascending order
list1 = [24,8,10,1,23]
list1.sort()
print(list1) # [1, 8, 10, 23, 24]
list1.sort(reverse=True)
print(list1) # [24, 23, 10, 8, 1]
1.5 Copy
copy(): Copy
Syntax:
new list sequence = original list sequence.copy()
1.6 List traversal
while traversal:
list1 = ["Tom","Jerry","Jack","James","Ray","Paul"]
i = 1
while i < len(list1) :
print(list1[i],end=" ")
i += 1
for traversal:
for i in list1 :
print(i,end=" ")
List summary:
common methods:
index(): return the subscript where the specified data is located, and report an error if it does not exist.
len(): access list length, that is, the number of data in the list
append(): append data at the end of the list
pop(): delete Specify the subscript data (the default is the last one), and return to change the data
remove(): remove the first match of a data in the list
2. Tuples
Multiple data can be stored in a tuple, and the data in the tuple cannot be modified
2.1 Format
(data 1, data 2, data 3...)
tuples are immutable data types
2.2 Operation
index(): return the index of the location of the specified data, if it does not exist, report an error
count(): count the number of times the specified data appears in the current list
len(): access list length, that is, the number of data in the list
tuple There are lists that can be modified
tuple1=(["Tom","Jerry"],2,3,4)
tuple1[0][0] = "Jack"
print(tuple1) # (['Jack', 'Jerry'], 2, 3, 4)
3. Dictionaries
The data in the dictionary is in the form of key-value pairs. The dictionary data has nothing to do with the order of the data, that is, the dictionary does not support subscripts. No matter how the data changes later, you only need to look up the data according to the name of the corresponding key to make the
dictionary variable. Types of
dict1 = {
'name':'Tom','age':20,'gender':'男'}
dict2 = {
} # 空字典
3.1 Add
Syntax:
dictionary sequence [key]=value
Note:
if the key exists, modify the value corresponding to this key; if it does not exist, add this key-value pair
dict1 = {
'name':'Tom','age':20,'gender':'男'}
dict1['age'] = 18
print(dict1) # {'name': 'Tom', 'age': 18, 'gender': '男'}
dict1['id'] = 24
print(dict1) # {'name': 'Tom', 'age': 18, 'gender': '男', 'id': 24}
3.2 Delete
del()/del: delete the dictionary or delete the specified key-value pair in the dictionary
dict1 = {
'name':'Tom','age':20,'gender':'男'}
del dict1['gender']
print(dict1) # {'name': 'Tom', 'age': 20}
clear(): Clear the dictionary
3.3 Find
key value search:
dict1 = {
'name':'Tom','age':20,'gender':'男'}
print(dict1['name'])
get()Search:
Syntax:
dictionary sequence.get(key, default value)
Note:
If the currently searched key does not exist, the second parameter is returned, and if the second parameter is omitted, it returns None
dict1 = {
'name':'Tom','age':20,'gender':'男'}
print(dict1.get('name')) # Tom
print(dict1.get('id',24)) # 24
print(dict1.get(('id'))) # None
keys(): Find the key value and return an iterable object
dict1 = {
'name':'Tom','age':20,'gender':'男'}
print(dict1.keys()) # dict_keys(['name', 'age', 'gender'])
values(): Find value value, return iterable object
dict1 = {
'name':'Tom','age':20,'gender':'男'}
print(dict1.values()) # dict_values(['Tom', 20, '男'])
items(): Find key-value pairs and return an iterable object
dict1 = {
'name':'Tom','age':20,'gender':'男'}
print(dict1.items()) # dict_items([('name', 'Tom'), ('age', 20), ('gender', '男')])
3.4 Traversal
Traverse key:
dict1 = {
'name':'Tom','age':20,'gender':'男'}
for key in dict1.keys():
print(key)
Traverse value:
dict1 = {
'name':'Tom','age':20,'gender':'男'}
for value in dict1.values():
print(value)
Traverse the elements:
dict1 = {
'name':'Tom','age':20,'gender':'男'}
for item in dict1.items():
print(item)
Traverse key-value pairs:
dict1 = {
'name':'Tom','age':20,'gender':'男'}
for key,value in dict1.items():
print(f'{key}={value}') # name=Tom age=20 gender=男
4. Collection
To create a collection, use {} or set(), but if you want to create an empty collection, you can only use set(), because {} is used to create an empty dictionary.
Collection features:
collections have de-duplication functions.
Collections do not support subscripts
s1 = {
24,4,5,6,72}
s2 = set('adsgf')
4.1 Add
add():
s1 = {
10,20}
s1.add(24)
update(): The appended data is a sequence
Note:
update can only append a sequence,
add can only append a single element
s1 = {
10,20}
s1.update([24,23])
s1.update(30) # 错误
print(s1) # {24, 10, 20, 23}
4.2 Delete
remove(): delete the specified data in the collection, if the data does not exist, an error will be reported
discard(): delete the specified data in the collection, if the data does not exist, no error will be reported
pop(): randomly delete one of the collections Data and return this data
s1 = {
10,20,30,40,50,60}
s1.remove(20)
s1.discard(10)
s=s1.pop()
4.3 Find
in: Determine whether the data is in the set sequence