Case: How to filter out numbers greater than 0 in the following list data
data = [1, -1, 2, 3, 4, 7]复制代码
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))复制代码
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])复制代码
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}复制代码
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])复制代码
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)复制代码
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)]复制代码
Plan: Define a dict (dictionary), Then iterate through the array.
c = dict.fromkeys(data, 0)复制代码
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复制代码
Solution: Use the Counter function in the collections package
c2 = Counter(data)复制代码
This solution can also easily get the top few that appear the most.
c2.most_common(3)复制代码
Problem: Sort the values in the following dictionary
cj = {x: randint(60, 100) for x in 'xyzabc'}复制代码
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])复制代码
Note: This function returns a new dictionary object
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)复制代码
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))}复制代码
This task can be accomplished using the intersection operation of sets.
s1.keys() & s2.keys() & s3.keys()复制代码
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!