1. Data container
Data container in Python: a data type that can hold multiple pieces of data. Each element contained can be any type of data, such as strings, numbers, Boolean, etc.
2. list list
subscript index of list
Subscripts for nested lists
my_list=['aaa',1,'True']
print(my_list[0])
print(my_list[1])
print(my_list[2])
my_list=['aaa',1,'True']
print(my_list[-1])
print(my_list[-2])
print(my_list[-3])
(1), index method
(2) Modification of the list
列表【下标】=值
(3), insert method
(4) Appending a single element at the end
(5) Appending elements in batches at the end
(6) Element deletion
my_list=['aaa','bbb','ccc']
del my_list[0]
print(my_list)
(7) remove method
Remove the first occurrence of an element in the list
my_list=['aaa','bbb','ccc']
my_list.remove('aaa')
print(my_list)
(8) clear method
(9) count method
Count the number of elements in a list
(10), len method
3. Overview of list methods
4. Practice
list_mess=[21,25,21,23,22,20]
list_mess.append(31)
print(list_mess)
fir_list=list_mess[0]
print(fir_list)
last=list_mess[-1]
print(last)
index=list_mess.index(31)
print(index)
5. Traversal of lists
my_list=['beijing','shanghai','hangzou']
index=0
while index<len(my_list):
ele=my_list[index]
print(f"列表的元素{ele}")
index+=1
my_list=[1,2,3,4]
for ele in my_list:
print(f"列表的元素有{ele}")
# list=[]
# list_num=[1,2,3,4,5,6,7,8,9,10]
# for ele in list_num:
# if (ele%2==0):
# list.append(ele)
#
# print(f"从列表{list_num}中取出偶数,组成新的列表{list}")
index=0
list=[]
list_num=[1,2,3,4,5,6,7,8,9,10]
while(index < len(list_num)):
ele=list_num[index]
index+=1
if(ele%2==0):
list.append(ele)
print(f"从列表{list_num}中取出偶数,组成新的列表{list}")
(f"从列表{list_num}中取出偶数,组成新的列表{list}")
6. Tuple
Tuple definition: Use parentheses to define a tuple, and use commas to separate each data. The data can be of different data types.
7. Tuple related operations
tu1=(1,2,3,3,3)
num=tu1.index(2)
print(num)
tot=tu1.count(3)
print(tot)
num2=len(tu1)
print(num2)
tu1=(1,2,3,3,3)
num=tu1.index(2)
print(num)
tot=tu1.count(3)
print(tot)
num2=len(tu1)
print(num2)
index=0
while( index<len(tu1)):
print(tu1[index])
index+=1
for ele in tu1:
print(ele)
Characteristics of tuples
8. String
Summary of commonly used operations on strings
Characteristics of strings
my_str="caicai studying"
result=my_str.count("caicai")
print(result)
resu1=my_str.replace(" ","|")
print(resu1)
resu2=resu1.split("|")
print(resu2)
9. Sequence
The operation on the sequence will not affect itself, but will result in a new sequence.
my_list=[0,1,2,3,4]
result1=my_list[1:4:1]
print(result1)
my_tuples=(1,2,3,4,5)
result2=my_tuples[::2]
print(result2)
my_str="1234556"
result3=my_str[::2]
print(result3)
my_str="希望今年有个offer,希望好运"
result1=my_str[::-1][5:10]
print(result1)
result2=my_str[6:11][::-1]
print(result2)
10. Use of collections
Sets are unordered, so sets do not support subscript index access, but sets, like lists, are allowed to be modified.
Add new element
Remove element
Pick out an element at random
my_set={
"aaa","bbb","ccc"}
ele=my_set.pop()
print(ele)//aaa
Clear collection
Difference of sets
set1={
1,2,3}
set2={
2,3,4}
set3=set1.difference(set2)#集合1有而集合2没有的
print(set3)#{1}
Eliminate the difference set of sets
set1={
1,2,3}
set2={
1,3,4}
set1.difference_update(set2)
print(set1)#{2}
print(set2)#{1,3,4}
Merge collection
set1={
1,2,3}
set2={
2,3,4}
set3=set1.union(set2)
print(set3)#{1,2,3,4}
Number of statistical collections
set1={
1,2,3,4,5}
res=len(set1)
print(res)#5
Summary of commonly used functions of collection
How to iterate over a collection
- You can use a for loop to traverse
- You cannot use a while loop because subscript indexing is not supported.
Collection features
my_list1=[1,2,3,4,3,3,5]
list1=set()
for x in my_list1:
list1.add(x)
print(list1)#{1,2,3,4,5}
10. Dictionary
Definition of dictionary: {} is also used, but the stored elements are key-value pairs, as follows:
my_dir={
"张三":19,"李四":20,"王五":21}
age=my_dir["张三"]
print(age)//19
11. Common operations on dictionary
Summary of commonly used dictionary operations
my_dir={
"王力宏":
{
"部门":"科技部",
"工资":3000,
"级别":1
},
"王力":
{
"部门":"科技部",
"工资":3500,
"级别":3
},
"王宏":
{
"部门":"研发部",
"工资":4000,
"级别":2
}
}
print(my_dir)
12. Classification and comparison of data containers
13. General statistical functions of data containers
Universal sorting function for containers (the sorted result will become a list object)
my_lisy= [2,1,11,3,5]
tota=sorted(my_lisy)
print(tota)//[1, 2, 3, 5, 11]