1. Sequence type definitions
A sequence is a group of elements with a sequential relationship
- Sequences are one-dimensional vectors of elements that can have different element types
- Sequence of similar math operations: S0, S1, ..., S(n-1)
- Elements are guided by serial numbers, and specific elements of the sequence are accessed through the following table
Sequence is a base class type
Sequence Processing Functions and Methods
Sequence type generic functions and methods
2. Basics of lists
2.1 List definitions
List: a variable sequence of data, but also a collection that can store a variety of data types, with parentheses ([ ]) to indicate the beginning and end of the list, the elements of the list are separated by a semicolon comma (,), and each element of the list has a corresponding subscript.
1) Lists are an extension of the sequence type and are very commonly used
- A list is a sequence type that can be modified at will after it is created.
- Created using [ ] or list(), with elements separated by English commas
- Each element of the list can be of a different type, infinite length
a = [] #Empty list a = [1,2,3,None,'bdshsvh',[1,2,3,5]]
The data in the list can be any data supported by python.
2) Subscripts for lists
The lower table of the list is also represented from 0, and the reverse from -1.
2.2 Basic list operations
Lists support adding, finding, modifying, deleting, and merging operations on collection elements.
1) Addition of list elements
append() method: A new element is added to the end of the list of existing elements.
>>> fruits = ['apple',5,'peach',2,'watermelon',12] #Original list >>> ('pear') # Add a new element with the append() method >>> (15) >>> print(fruits) ['apple', 5, 'peach', 2, 'watermelon', 12, 'pear', 15]
insert() method: you can add an element to the list at any specified position. insert() method's first parameter is the table of the list where the element needs to be inserted, and the second parameter is the value of the specified new element.
>>> (0,'starfruit') >>> (1,14.2) >>> print(fruits) ['starfruit', 14.2, 'apple', 5, 'peach', 2, 'watermelon', 12, 'pear', 15]
2) List element search
index () method: syntax format (value, [start [, stop]]), where L is a list of objects, value on behalf of the need to find the elements in the list, start on behalf of the elements in the list to find the beginning of the search for the number of subscripts, stop on behalf of the end of the search for the number of subscripts, start and stop parameters can be selected. If the element is found, then return the first element found, if not found, then return "ValueError..." error message.
>>> ("pear") 8 >>> (1) Traceback (most recent call last): File "<pyshell#15>", line 1, in <module> (1) ValueError: 1 is not in list >>> (2,5,12) 5
in membership operator judgment
>>> 'a' in fruits False
The advantage of judging with in is that no error message is generated.
Read the corresponding element with the subscript
>>> fruits[5] 2 >>> fruits[8] 'pear'
Slice reading
>>> fruits[5:] [2, 'watermelon', 12, 'pear', 15]
3) List element modification
A list is modified by assigning a value to the corresponding element by specifying a subscript.
>>> ls = ['Tom',1,5,8] >>> ls[2] = 8 >>> ls ['Tom', 1, 8, 8] >>> ls[3] = 'Yuan' >>> ls ['Tom', 1, 8, 'Yuan']
4) List element modification
clear() method: Clear all elements in the list.
>>> () >>> fruits [] >>> len(fruits) 0
pop() method: Syntax format ([index]), L represents the list, index is an optional parameter, when the index parameter is specified, the element at the corresponding subscript position will be deleted (if the value of the specified parameter is not in the number of subscripts in the list, an error will be reported). pop() defaults to deleting the first element at the end of the list.
>>> listpop = ['Ball 1','Ball 2','Ball 3'] >>> get_one = () >>> print(get_one,'',listpop) Ball 3 ['Ball 1', 'Ball 2'] >>> (2) Traceback (most recent call last): File "<pyshell#29>", line 1, in <module> (2) IndexError: pop index out of range
remove() method:(value), L represents the list, value represents the element to be deleted, if there are more than one element to be deleted, only the first one on the left will be deleted.
>>> listpop = ['Ball 1','Ball 2','Ball 3','Ball 2'] >>> ('Ball 2') >>> listpop ['Ball 1', 'Ball 3', 'Ball 2'] >>>
del function (math.): You can delete the specified element or the whole list.
>>> del(listpop[2]) >>> listpop ['Ball 1', 'Ball 3'] >>> del(listpop) >>> listpop Traceback (most recent call last): File "<pyshell#36>", line 1, in <module> listpop NameError: name 'listpop' is not defined >>>
5) List element merging
For merging two list objects, you can use the extend() method
>>> team1 = ["Zhang San","Li Si.","Wang Wu"] >>> team2 = ["Tom","John"] >>> (team2) >>> team1 ['Zhang San', 'Li Si', 'Wang Wu', 'Tom', 'John'] >>>
The merge of two list objects can also be achieved by using "+", but it will change the memory address of team1, indicating that team1 has been redefined after the merge and is no longer the original team1.
>>> team1 = ["Zhang San","Li Si.","Wang Wu"] >>> team2 = ["Tom","John"] >>> id(team1) 2172213272840 >>> (team2) >>> id(team1) 2172213272840 >>> team1 = team1 + team2 >>> team1 ['Zhang San', 'Li Si', 'Wang Wu', 'Tom', 'John'] >>> id(team1) 2172213337736
6) Sort list elements
sort() method: (key=None,reverse=False), L represents the list, key is an optional parameter, used to specify in the comparison before calling what kind of function on the elements, such as key = (will convert uppercase letters to lowercase letters function), reverse is an optional parameter, the default is the incremental (generally based on ASCII code) sorting. (generally based on the ASCII code from small to large characters, numbers are sorted) sorting.
>>> fruits = ['banana','pear','apple','peach'] >>> fruit_1 = () >>> fruit_1.sort() >>> fruit_1 ['apple', 'banana', 'peach', 'pear'] >>> fruit_h = () >>> fruit_h.sort(reverse=True) >>> fruit_h ['pear', 'peach', 'banana', 'apple'] >>>
fruits = ['banana','pear','Apple','peach'] >>> fruit_s = () >>> fruit_s ['Apple', 'banana', 'peach', 'pear']
(7) List of other methods of operation
copy() method: Make a copy of the list.
>>> fruit_s = () >>> fruit_s ['banana', 'pear', 'Apple', 'peach'] >>> id(fruits) 2172213342792 >>> id(fruit_s) 2172213273608
count() method: Counts the number of specified elements of a list.
>>> vegetable = ['Cabbage','Green vegetables','Radish','Celery','Spinach','Cabbage','Cauliflower'] >>> ('Cabbage') 2
reverse() method: Permanent reverse logging of list objects.
>>> vegetable = ['Cabbage','Green vegetables','Radish','Celery','Spinach','Cabbage','Cauliflower'] >>> () >>> vegetable ['Cauliflower', 'Cabbage', 'Spinach', 'Celery', 'Radish', 'Green vegetables', 'Cabbage'] >>>
list resolution
Grammar:
[expression for iter_val in iterable] [expression for iter_val in iterable if cond_expr]
Description: expression is an element-based arithmetic expression, such as i**2, squared for each element, iter_val is the element i obtained iteratively from the list iterable, the if clause judges the element, and cond_expr is the judgment element expression.
>>> nums = [i**2 for i in range(11) if i>0] >>> nums [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] >>>
Equivalent:
>>> nums =[] >>> for i in range(1,11): (i**2) >>> nums [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] >>>
summarize
That's all for this post, I hope it helped you and I hope you'll check back for more from me!