当前位置:网站首页>Use of list - addition, deletion, modification and query

Use of list - addition, deletion, modification and query

2022-04-23 17:45:00 HXX-LYX

1. Concept

1.1 add to

1.1.1    list .append(" Data and information ")                          Add... To the end of the list " Data and information "

1.1.2    list .insert(" Index value "," To add information ")            Add information to the specified index of the list

1.1.3    list .extend("list2")                                  take list2 Add list information to list1 in

1.2 modify

1.2.1    list [ Index the following table ]= Latest value                               Modify the index value specified in the list to the latest value defined

1.3 see

1.3.1   print( list )                                                  View the complete list information

1.3.2   print( list [index])                                      View the specified index subscript < value >

1.3.3   print( list .index(" Data and information "))                      View the current " Data and information " Corresponding index subscript

1.4 Delete

1.4.1    list .pop()                                                  Delete the last data message

1.4.2    list .pop(index)                                          Delete pop The value information corresponding to the index

1.4.3    list .remove(" Data and information ")                            Delete the specified data information

1.4.4    list .clear()                                                  Clear the list information

1.4.5   del list                                                       Delete the current list

1.5 Sort

1.5.1    list .sort()                                                  Sort the list

2. Concrete practice

2.1 add to

Add list information backward ,append You can only append one by one

name_list = ["zhangsan", "lisi", "wangwu"]
name_list.append("zhaoliu")
print(name_list)

"C:\Program Files (x86)\python.exe" L:/project/01-python Basic information / List information / Add list information .py
['zhangsan', 'lisi', 'wangwu', 'zhaoliu']
Add information at the specified location insert( Indexes , Information )
name_list1 = ["zhangsan", "lisi", "wangwu","zhaoliu"]
name_list1.insert(2, "maqi")
print(name_list1)

"C:\Program Files (x86)\python.exe" L:/project/01-python Basic information / List information / Add list information .py
['zhangsan', 'lisi', 'maqi', 'wangwu', 'zhaoliu']
take name_list1 Information added to name_list2 in extend( Additional list name information )

name_list2 = ['zhangsan', 'lisi', 'maqi', 'wangwu', 'zhaoliu']
name_list2.extend(name_list1)
print(name_list2)

"C:\Program Files (x86)\python.exe" L:/project/01-python Basic information / List information / Add list information .py
['zhangsan', 'lisi', 'maqi', 'wangwu', 'zhaoliu', 'zhangsan', 'lisi', 'maqi', 'wangwu', 'zhaoliu']

2.2 modify

Modify the list list [ Indexes ]= Modify value information

name_list5 = ['zhangsan', 'lisi', 'maqi', 'wangwu', 'zhaoliu']
name_list5[2] = 999
print(name_list5)

"C:\Program Files (x86)\python.exe" L:/project/01-python Basic information / List information / Modify list information .py
['zhangsan', 'lisi', 999, 'wangwu', 'zhaoliu']

2.3 see

Output list information directly

name_list = ["zhangsan", "lisi", "wangwu"]
print(name_list)

"C:\Program Files (x86)\python.exe" L:/project/01-python Basic information / List information / View a list of information .py
['zhangsan', 'lisi', 'wangwu']
# View a string information in the list name_list[1]

name_list = ["zhangsan", "lisi", "wangwu"]
print(name_list[1])

"C:\Program Files (x86)\python.exe" L:/project/01-python Basic information / List information / View a list of information .py
lisi
View the index information corresponding to a string in the list name_list.index("wangwu")
name_list = ["zhangsan", "lisi", "wangwu"]
print(name_list.index("wangwu"))

"C:\Program Files (x86)\python.exe" L:/project/01-python Basic information / List information / View a list of information .py
2

2.4  Delete

Delete the last string list .pop() pop The method can be understood as similar to taking something out of a barrel

name_list5 = ['zhangsan', 'lisi', 'maqi', 'wangwu', 'zhaoliu']
name_list5.pop()
print(name_list5)

"C:\Program Files (x86)\python.exe" L:/project/01-python Basic information / List information / Delete list information .py
['zhangsan', 'lisi', 'maqi', 'wangwu']
Delete the information corresponding to the index

 name_list6 = ['zhangsan', 'lisi', 'maqi', 'wangwu', 'zhaoliu']
name_list6.pop(2)
print(name_list6)

"C:\Program Files (x86)\python.exe" L:/project/01-python Basic information / List information / Delete list information .py
['zhangsan', 'lisi', 'wangwu', 'zhaoliu']
Delete the specified data information list .remove(" String information ")

name_list4 = ['zhangsan', 'lisi', 'maqi', 'wangwu', 'zhaoliu']
name_list4.remove("zhaoliu")
print(name_list4)

"C:\Program Files (x86)\python.exe" L:/project/01-python Basic information / List information / Delete list information .py
['zhangsan', 'lisi', 'maqi', 'wangwu']
Clear all the list information list .clear()

name_list3 = ['zhangsan', 'lisi', 'maqi', 'wangwu', 'zhaoliu']
name_list3.clear()
print(name_list3)

"C:\Program Files (x86)\python.exe" L:/project/01-python Basic information / List information / Delete list information .py
[]
Delete list

name_list2 = ['zhangsan', 'lisi', 'maqi', 'wangwu', 'zhaoliu']
del name_list2
print(name_list2)

"C:\Program Files (x86)\python.exe" L:/project/01-python Basic information / List information / Delete list information .py
Traceback (most recent call last):
  File "L:/project/01-python Basic information / List information / Delete list information .py", line 8, in <module>
    print(name_list2)
NameError: name 'name_list2' is not defined

2.5 Sort

Sort ----- In reverse order , Descending

#  Sort the list   sort()  Default from small to large 
name_list2 = ['zhangsan', 'lisi', 'maqi', 'wangwu', 'zhaoliu']
name_list2.sort()
print(name_list2)

#  Descending the list   sort(reverse=True)
name_list3 = ['zhangsan', 'lisi', 'maqi', 'wangwu', 'zhaoliu']
name_list3.sort(reverse=True)
print(name_list3)


#  Reverse the list   reverse()
name_list4 = ['zhangsan', 'lisi', 'maqi', 'wangwu', 'zhaoliu']
name_list4.reverse()
print(name_list4)

"C:\Program Files (x86)\python.exe" L:/project/01-python Basic information / List information / List sorting information .py
['lisi', 'maqi', 'wangwu', 'zhangsan', 'zhaoliu']
['zhaoliu', 'zhangsan', 'wangwu', 'maqi', 'lisi']
['zhaoliu', 'zhangsan', 'wangwu', 'maqi', 'lisi']

版权声明
本文为[HXX-LYX]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231743456462.html