sequence:
List[,,,]
Tuple (,,,,), the content cannot be changed. If the tuple has only one element, it is expressed as (x,) and a comma must be added after it
tuple() function: Pass the sequence as a parameter to this function, and the parameter will be returned as is
String
Common operations on sequences:
(1)Index
Get the relative elements of the sequence according to the index, 0 means the first one starting from the first one, -1 means the first one from the last
(2) Sharding
Perform interval values on the elements in the sequence. Take a certain range of elements, 12
1. Elegant shortcut
number[:2] //Indicates taking the first 2 numbers
number:[-2:0] //Indicates the last two numbers
number[:] //Indicates taking the entire sequence
2. Note:
number[2:-4] //The sequence represented by this slice does not include the element located at the -4 position
3. You can set the step size. In sharding, from the start index to the end index, the default step size is 1. You can set the step size by setting the third parameter, for example
number[2:5:2] means from the third element to the sixth element, get every 2 elements,
The step size cannot be 0
(3) Sequence addition
Just find the intersection
(4)Multiplication
Repeat the number of times each element in the set is multiplied
[None]*10 means this is a sequence of ten elements. But there is nothing at the position of each element
(5) Determine membership
>> x=[1,2,3,4,5,6]
>>2 in x //Judge whether 2 is in sequence x
>>True //Output result
(6) Length, minimum value, maximum value
>>x=[1,2,3,4,5]
>>len(x) //Find the length and height of the sequence
>>5 //Output results
>>max(x) //Find the maximum value of the sequence
>>5
>>min(x) //Find the minimum value in the sequence
>>1
list('Hello') //The list function can turn a string sequence into a list
>>['H','e','l','l','o']
Operations on lists:
(1) Change list: element assignment
x=[1,2,3,4,5]
x[2] = 00 //Change the element value in the sequence through direct assignment
x=[1,2,00,4,5]
(2) Delete list
x=[1,2,3,4,5]
del x[2] //Delete the third element
x=[1,2,4,5]
(3) Slice assignment
The assigned value can be different from the number of elements in the original fragment, or it can be empty, and its effect is equivalent to deletion
x=[1,2,3,4,5]
x[-2:] = list('ok')
x = [1,2,3,o,k]
List method:
1. append method:
>>x=[1,2,3,4,5]
>>x.append(666) //Add new elements to the end of the list
>>x=[1,2,3,4,5,666]
2. count statistics
>>x=[1,2,3,2,4,5]
>>x.count(2) counts how many times it appears in the sequence
>>2
3. extend extension method
>>x=[1,2,3,4,5]
>>y=[a,b,c,d]
x.extend(y) //On the basis of x, the extension adds the sequence element of y. The difference from the + connector is that extension changes the extended sequence, while + gets a new one Sequence, the original added parties have not changed
x=[1,2,3,4,5,a,b,c,d]
4. index gets the index position
>>x=[1,2,3,4,5]
>>x.index(3) //Get the index position of element 3
>>2
5. insert insertion method (difference from append: append can only be added to the end and cannot specify the index position)
x=[1,2,3,4,5]
x.insert(2,'ok') //Insert 'ok' at index position 2
x=[1,2,3,'ok',4,5]
6. Pop deletion method:
x=[1,2,3,4,5]
x.pop() // Delete the last element by default
5 //And return the deleted element
x=[1,2,3,4]
x.pop(2) //Delete the element at the specified position according to the given position
3 //And return the deleted element
x=[1,2,4]
7. Remove removal method (associate index method, the parameter is the element, index returns the first matching index value, remove directly deletes the element)
x=[1,2,3,4,5]
x.remove(4)
x=[1,2,3,5]
8. reverse stores the element values in the list in reverse. You can also use the reversed() function to change the function and pass the list as a parameter
x=[1,2,3,4,5]
x.reverse()
x=[5,4,3,2,1]
9. sort, change the original list
x=[1,2,3,4,5]
x.sort()
x=[5,4,3,2,1]
Extension: Get a copy of the sorted sequence
sorted(x) function takes the sorted sequence as a parameter
10. Advanced sorting
By passing in keyword parameters to the list sort() method, different types of sorting are achieved
x=[1,2,3,4,5]
x.sort(cmp) Sort by numerical value
x=['x','xx','xxx','xxxx']
x.sort(key=len) Sort by the length of the list elements
x.sort(reverse=True/False) //Whether to perform reverse sorting
The above is the detailed content of [Learning and Organizing] Chapter 2 Lists and Origins. For more information, please follow other related articles on the PHP Chinese website!