**Sequence** data type of Python basic learning

Sequence data type of Python basic learning (members are arranged in order and can be accessed by subscript displacement)

1. Sequence type operators

    • dict() dictionary

Two ways to judge whether a key is in the dictionary:
1. key in dict: return True or False
2. dict.get() method: return the corresponding value if it exists; otherwise return None (empty value, nothing is returned) You can also specify the return value dict.get('Thomas', -1) cannot return -1It is located by hash algorithm

Delete one of the elements, dict.pop(key), delete the entire key-value pair. There is no order inside the dict
dict can be used in many places where high-speed search is required

The for loop can actually use two or more variables at the same time. For example, items() of dict can iterate key and value at the same time:

for k, v in d.items():
//k,v可以同时迭代字典的key和value

Compared with list, dict has the following characteristics:

1. The speed of search and insertion is extremely fast, and will not slow down as the key increases;
2. It needs to occupy a lot of memory, and there is a lot of memory waste.
And list is the opposite:

1. The time of searching and inserting increases with the increase of elements; it
takes up little space and wastes little memory.

2. Therefore, dict is a way to exchange space for time.

    • set takes list as the input set: (duplicate data can be automatically filtered)

    • in,not in

2. Python's string parameter passing problem

1. %s method, %d, %f, %x

// 
print('Age: %s. Gender: %s' % (25, True)) (当需要输出%号时,用两个%%);

2. format() method

// 
print('Hello, {0}, 成绩提升了 {1:.1f}%'.format('小明', 17.125));

It will replace the placeholders {0}, {1}...

2. The f-string method
{s:.2f} is replaced by the value of the variable s, and: the following .2f specifies the formatting parameters (that is, two decimal places are reserved), so the replacement result of {s:.2f} is 19.62.

3. Function parameter passing problem

3.1 Variable parameters (As the name implies, variable parameters means that the number of parameters passed in is variable, it can be 1, 2 to any number, or 0.)

insert image description here
A * sign is added in front of the parameter. Inside the function, the parameter numbers receives a tuple.

It is allowed to add an * in front of the list or tuple to turn the elements of the list or tuple into variable parameters:

3.2 Keyword arguments


insert image description here
insert image description here
Allows you to pass in 0 or any number of parameters with parameter names, and these keyword parameters are automatically assembled into a dict simplified notation inside the function.
insert image description here
For keyword parameters, the caller of the function can pass in any unlimited keyword parameters. As for what is passed in, you need to pass the kw check inside the function.
insert image description here

4. Advanced functions

4.1 map function

The map() function receives two parameters, one is a function and the other is an Iterable. map applies the incoming function to each element of the sequence in turn, and returns the result as a new Iterator.
insert image description here

4.2filter function (filter function)

filter() also takes a function and a sequence. Unlike map(), filter() applies the incoming function to each element in turn, and then decides whether to keep or discard the element according to whether the return value is True or False.
insert image description here

4.3sorted function (sorting function)

The default is to sort from small to large
1. Sort the list directly
insert image description here

2. Sorting by receiving parameters Sorting
by absolute value
The function specified by key will act on each element of the list, and sort according to the result returned by the key function.
insert image description here

3. Reverse sorting
insert image description here

Guess you like

Origin blog.csdn.net/baidu_41810561/article/details/120616192