Common operations on lists in python
Indexing and Slicing Lists
What is an index?
Strings, lists, and tuples are indexed
The index is the position of the element recorded from the leftmost, expressed as a number, starting from 0
Maximum index of string, list, tuple = length - 1
list = ['xiaoming', 'xiaohong'] print(list[0]) # 0is the index value,Returns the index value0corresponding element
What's a slice?
Indexes are used to access a single element, while slices access a range of elements
The slice looks up the elements between the two indexes that are separated by a colon inside the middle parentheses, as in[0: 10]
The slicing rules arekeep one's fingers crossed
list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] print(list[1: 3]) # Returns a list of elements corresponding to index values 1 to 3 (excluding 3) print(list[:]) # with nothing on either side of the colon returns a list of all the elements) print(list[0:]) # This also returns a list of all elements print(list[:-1]) # Returns a list of all elements except the last one. The rightmost index is -1, followed by -2, and -3, if indexed from right to left. print(list[::-1]) # Returns a reverse-ordered list print(list[-3:-1]) # Returns a list of the 2nd and 3rd elements from the right to the left (indexed at -2, -3), since the slicing rule is left inclusive, right exclusive, so it will not contain elements indexed at -1 print(list[1:3:2]) # 2 represents the step size, jumping to get a list of elements print(list[0:0]) # Returns an empty list
Attention:The list generated after slicing is a new list (even if, like the original list, it is a new variable)
Example:
List assignment by index/slice and index fetching
list[index] = new_item list[0:2] = [1, 2]
Data can only be modified within the scope of an existing index, and cannot be assigned by adding a new index.
The index() function can be used to find the index value of the corresponding element.
Example:
The pop function removes elements
Remove and fetch elements of a list by index
(index) # indexis the index of the element you want to delete,and returns the element at that index
Report an error if the passed-in index does not exist:
Direct deletion, no return value
del list[index] # indexis the index of the element you want to delete
If the index does not exist, an error is reported:
Indexing & slicing specialization in tuples
Identity:
- Tuples and lists alike get indexes and slice indexes
- The index function is used in tuples in the same way as it is used in lists.
Specificity:Tuples cannot be indexed to modify and delete elements
String indexing and slicing
String indexing and slicing
Each character of a string corresponds to an index, and the indexing rules are the same as for lists.
Strings are also sliced in the same way as lists
Attention:Strings cannot be modified or deleted by indexing, and strings cannot be modified.
String find () and index () function
Gets the index position of the element:
(item) # Return the index of the item element, if item is more than one character, then return the index of the first character. (item) # # come (or go) backitemIndex of the element,in the event thatitemis more than one character,则come (or go) back第一个字符的索引
The difference between the two functions:find function can not get, will return -1; index function if you can not find, directly report errors
To this point this article on python list of common operations summarizes the article is introduced to this, more related python list of operations content please search my previous posts or continue to browse the following related articles I hope you will support me in the future more!