Home > Backend Development > Python Tutorial > Data structure written for Python programming masters

Data structure written for Python programming masters

coldplay.xixi
Release: 2020-11-04 17:13:54
forward
1766 people have browsed it

python video tutorial column introduces the key points that need to be paid attention to in Python programming.

Data structure written for Python programming masters

How to filter data based on conditions in lists, dictionaries, and collections

Case: How to filter out numbers greater than 0 in the following list data

data = [1, -1, 2, 3, 4, 7]复制代码
Copy after login

Use the filter function. The first parameter is a function, or you can pass in a lambda expression like the following code.

list(filter(lambda x: x >= 0, data))复制代码
Copy after login

Another solution is to use list generation as follows. This solution is more efficient than the previous solution.

print([x for x in data if x >= 0])复制代码
Copy after login

In a data structure such as a dictionary, similar dictionary productions can also be used.

from random import randint
d = { x: randint(60, 100) for x in range(1, 21)}
{k:v for k, v in d.items() if v >= 90}复制代码
Copy after login

How to name each element in the tuple to improve program readability

Solution: Define constants and use constants to index elements in the tuple, such as the following code

stu = ('hao', 18, 'male', '1078244513@qq.com')
NAME = 0AGE = 1SEX = 2print(stu[SEX])复制代码
Copy after login

Solution: Use the namedtuple function of the collections package. This function will return the definition of a new "class" and is used as follows.

from  collections import  namedtuple
Stu = namedtuple('Stu', ['name', 'age', 'sex', 'email'])# stu = Stu('hao', 18, 'male', '1078244513@qq.com')stu = Stu(name='hao', age=18, sex='male', email='1078244513@qq.com')

print(stu.email)复制代码
Copy after login

How to count the frequency of occurrence of elements in a sequence

Problem: Count the number of occurrences of each number in the following list of numbers

from random import randint
data = [randint(0, 20) for _ in range(30)]复制代码
Copy after login

Plan: Define a dict (dictionary), Then iterate through the array.

c = dict.fromkeys(data, 0)复制代码
Copy after login

The above code will generate a dictionary object with different values ​​in data as keys and 0 as the value.

for x in data:
    c[x] += 1复制代码
Copy after login

Solution: Use the Counter function in the collections package

c2 = Counter(data)复制代码
Copy after login

This solution can also easily get the top few that appear the most.

c2.most_common(3)复制代码
Copy after login

How to sort the items in the dictionary according to the size of the values ​​in the dictionary

Problem: Sort the values ​​​​in the following dictionary

cj = {x: randint(60, 100) for x in 'xyzabc'}复制代码
Copy after login

Scheme: in the sorted function As the second parameter, you can pass in a function object, and sorted will sort based on the return value of the function.

sorted(cj.items(), key=lambda item:item[1])复制代码
Copy after login

Note: This function returns a new dictionary object

How to quickly find the common key (key) in multiple dictionaries?

First of all, let’s introduce the key in python Sampling function sample, which is located in the random package. This takes a sequence type parameter and a number, and returns a sequence randomly sampled from the sequence. The following code.

from random import sample
sample('abcdefg', 3)复制代码
Copy after login

Question: How to get the common keys of the following three collections.

s1 = {x : randint(1, 4) for x in sample('abcdefg', randint(3, 6))}
s2 = {x : randint(1, 4) for x in sample('abcdefg', randint(3, 6))}
s3 = {x : randint(1, 4) for x in sample('abcdefg', andint(3, 6))}复制代码
Copy after login

This task can be accomplished using the intersection operation of sets.

s1.keys() & s2.keys() & s3.keys()复制代码
Copy after login

How to keep the dictionary in order?

The OrderedDict type of the collections package will maintain the order in which it is entered into the dictionary.

Related free learning recommendations: python video tutorial

The above is the detailed content of Data structure written for Python programming masters. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:juejin.im
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template