list tuple
Operators for list tuples
Use of len in list tuples
- The len function can calculate the length of all data types except numeric types
Accumulation and multiplication between lists (tuples)
- To add two lists you can use
+
- Multiple accumulation of the same list can be used
*
Usage of in and not in in lists (tuples)
- in is used to determine whether a member (element) is in the data structure
- not in is used to determine whether a member (element) is not in the data structure
append() function (adds an element to the current list)
- usage:
list.append(new_item)
- parameter
new_item:需要添加到列表中的元素
- Notice:
- A new list will not be generated, it will be added to the original list
- Elements being added will only be added to the end
insert() function (add an element to the specified position of the current list)
- usage:
list.insert(index,new_item)
- parameter
index:新元素的位置
new_item:需要添加到列表中的元素
- Notice:
- append can only be added to the end of the list, insert can choose any position
- If the index passed in by insert does not exist, a new element will be added to the end of the list
- The positions of strings, tuples, and list elements are counted from 0
count() function (returns the number of a member in the current list) (tuples are available)
- usage:
inttype = list.count(item)
- parameter
item:需要查询个数的元素
- Notice:
- Returns 0 if the queried member (element) does not exist
- Using this function, only complete elements in the list or tuple are checked for content that needs to be calculated:
- For example, "watermelon" exists in the list
- if coun("West")
- then return 0
remove() function (delete an element in the list)
- usage:
list.remove(item)
- parameter
item:准备删除的列表元素
- Notice:
- If the member specified to be deleted does not exist in the list, an error will be reported directly
- If there are multiple elements to be deleted, the first element from left to right will be deleted
- The remove function does not create a new list, but deletes it from the original list
python's built-in function del (delete completely)
- del can completely delete the variable
- usage:
del 变量名
- parameter
变量名:准备删变量
- Notice:
- If the deleted variable is reapplied, an error will be reported (variable is not defined)
reverse() function (reverse the current list order)
- usage:
list.reverse()
- parameter
无参数
- Notice:
- Make changes to the current list
sort() function (sort the current list according to certain rules)
- usage:
list.sort(cmp=NONE,key=NONE,reverse=False)
- parameter
cmp --- 可选参数,指定排序方案
key --- 参数比较
reverse --- 排序规则,reverse= True 降序,reverse = False 升序(默认)
- Notice:
- By default, sort according to the order of the first letter of the string and the size of the number
- It is necessary to ensure that the elements in the list are of the same type, otherwise they cannot be sorted and an error will be reported
- If the order of the current list meets the requirements, no further sorting will be performed
clear() function (clear all data in the current list)
- usage:
list.clear()
- parameter
无需传参
- Notice:
- There is no return value, it is to modify the existing list
copy() function (copy the current list to the same list)
- usage:
list.copy()
- parameter
无需传参
- Notice:
-
The content of the new list is the same as that of the old list, but the memory address pointed to is different, not the same list in essence
-
The variable assigned twice is the same as the original variable, and the memory address pointed to is also the same, essentially the same list
-
copy is a shallow copy (after a certain data in the list changes, it will be affected by each other)
# coding:utf-8 a = [[1,2,3],[4,5,6]] b = a.copy() print(b) b[0].append(4) print(a) print(b)
-
You can use copy.deepcopy(X) to make a deep copy (deep copy also copies the deep data, and the original data and new variables do not share data at all)
# coding:utf-8 import copy a = [[1,2,3],[4,5,6]] b = copy.deepcopy(a) print(b) b[0].append(4) print(a) print(b)
-
extend() function (import elements in other lists or tuples into the current list at one time)
- usage:
list.extend(iterable)
- parameter
iterable 代表列表或者元组,该函数无返回值
- Notice:
- The incoming parameter needs to be an iterable element
- The elements of the iterable will be imported into the list
List indexing and slicing
index
- strings, lists and tuples
- The position recorded from the left is the index
- The index is represented by numbers, starting from 0 from left to right
- The maximum index of strings, lists (tuples) is their length - 1
slice
- Indexing is accessing a single element, while slicing is accessing a range of elements
- Use the colon to find the two separate indexes in the square brackets
[0:3]
- Slicing rules, after demarcating the range, the left contains the right does not contain
- The list obtained by slicing is not the original list
- You can use
[::-1]
the way to reverse the list - You can use
[-3:-1]
the reverse way to get elements - Slices can be obtained by setting the step size
[起始元素:终止元素(不包含):步长]
- You can use
[0:0]
the way to generate an empty list
Index acquisition and modification
- usage:
list[index]
- parameter
index 代表一个在存在范围内的索引
- Notice:
- Data modification can only be within the index range
- Lists cannot add values by adding new indexes
- You can
list.index(item)
find the index value of the item element in the list by
pop() function (remove elements in the list according to the index)
- usage:
list.pop(index)
- parameter
index 代表一个在存在范围内的索引
- Notice:
- Data modification can only be within the index range, if it does not exist, an error will be reported
- The function removes the element at that index and returns
del() function (deletes a list or its index)
- usage:
del list[index]
- parameter
index 代表一个在存在范围内的索引
- Notice:
- This function will directly delete the value at the specified index position in the list without return value
- Report an error if index (index) does not exist
Specificity of indexing and slicing in tuples
- Tuples can get indexes and slice indexes like lists
- The usage of the tuple function index is exactly the same as that of the list
- It is not possible to modify and delete elements by modifying the index (tuples are immutable)
String indexing and retrieval
- Indexing of strings is similar to indexing of lists
- Each character in the string has an index position
- The rest of the rules are similar to list indexing
- Strings cannot be modified and deleted through indexes
- String cannot be modified
String find and index functions
- Both are used to get the index position of the element
- usage:
string.index(item)
string.find(item)
- parameter
item 希望查找索引位置的元素
- Notice:
- find can not find the corresponding element position will return -1
- If the index cannot find the position of the corresponding element, an error will be reported directly