Python efficient skill notes, data structure and algorithm

1. How to filter data according to conditions in dictionary, sequence and collection

Case study:

  • Filter out negative numbers in list [3,9, - 1...],
    Note here: whether you are for Python employment or hobbies, remember: project development experience is always the core. If you don't have the latest Python introduction to the advanced practical video tutorial in 2020, you can go to the small Python exchange : you can find a lot of new Python tutorial projects under the transformation of "seven clothes, nine seven buses and five numbers" (homophony of numbers). You can also communicate with the old driver for advice!

Solution: List resolution [x for X in data if x > = 0]

l=[randint(-10,10) for _in range(10)]

[x for x in l if x>=0]
 
g=filter(lambda x:x>=0,l)
next(g)
list(g)
Copy code

Case study:

  • Sift out items with values higher than 90 in {'LiLei': 79, 'jim':88,'Lucy':92...} dictionary

Solution: Dictionary parsing {K: V for K, V in d.items() if V > 90}

d={'student%d' % i:randint(50,100) for i in range(1,21)}
{k:v for k,v in d.items if v>=90}
g=filter(lambda item:item[1]>=90,d.items())
list(g)
dict(g)
Copy code

Case study:

  • Sift out the elements in the set {77,89,32,20...} that can be divided by 3
  • Solution: List resolution [x for X in data if x% 3 = = 0]

s={randint(0,20) for _ in range(20)}
{x for x in s if x % 3==0}
Copy code

2. How to name each element in the tuple to improve the readability of the program

Case study:

Data in student system is in fixed format: (name, age, gender, email)

('jim',16''male','jim8721@gmail.com')

('LiLei',17''male','leilei@qq.com')

('Lucy',16''female','lucy123@yahoo.com')

We use index to access, a large number of indexes reduce the readability of the program

Solution:

Scenario 1: define a series of numeric constants or enumeration types

Scenario 2: replace the built-in tuple with collections.namedtuple in the standard library

from enum import IntEnum
NAME,AGE,SEX,EMAIL=range(4)
s=('jim',16''male','jim8721@gmail.com')
class StudentEnum(IntEnum):
    NAME=0
    AGE=1
    SEX=2
    EMAIL=3
StudentEnum.NAME
s[StudentEnum.NAME]
isinstance(StudentEnum.NAME,int)
Copy code

Solution 2:

from collections import namedtuple
Student=namedtuple('Student',['name','age','sex','email'])
s2=Student('jim',16''male','jim8721@gmail.com')
s2
Student(name='jim',age=16,sex='male',email='jim8721@gmail.com')
isinstance(s2,tuple)
Copy code

3. How to sort the items in the dictionary according to the value size in the dictionary

Case study:

The English scores of a class are stored in the form of a dictionary:

{
    'LiLei':79,
    'jim'  :88,
    'Lucy' :92,
    ..
}
Copy code

How to calculate students' ranking according to their grades

Solution: convert the items in the dictionary to tuples and sort them with the built-in function sorted

Scenario 1: convert the items in the dictionary to (value, key) tuples. (list parsing or zip)

from random import randint
d={k:randint(60,100) for k in 'abcdefg'}
l=[(v,k) for k in d.items()]
sorted(l,reverse=True)
d.items()
p=sorted(d.items(),key=lambda item:item[1],reverse=True)
list(enumerate(p))
list(enumerate(p,1))
for i (k,v) item in enumerate(p,1):
    print(i,k,v)
    d[k]=(i,v)
//Dictionary parsing: {k:(i,v) for i (k,v) item in enumerate(p,1)}
Copy code

4. How to count the frequency of elements in the sequence

Case study:

1. In a random sequence [12,5,6,4,6,5,5,7], three elements with the highest frequency of occurrence are found. What are their frequency of occurrence?

2. For an English article, do word frequency statistics and find the 10 words with the highest frequency of occurrence. How many times do they appear?

Solution:

Scheme 1: convert the sequence to dictionary {element: frequency}, and sort according to the value in the dictionary.

from random import randint
data=[randint(0,20) for _ in range(30)]
d=dict.fromkeys(data,0)
for x in data:
    d[x] +=1
sorted(((v,k) for k, v in d.items()),reverse=True)[:3]
import heapq
heapq.nlargest(3,(v,k) for k,v in d.items)
Copy code

Scenario 2: use Counter object in standard library collections

from collections import Counter 
data 
c=Counter(data)
c.most_common(3)

txt=open('example.txt').read()
import re
word_list=re.split('\W+',txt)
c2=Counter(word_list)
c2.most_common(10)
Copy code

5. How to quickly find common keys in multiple dictionaries

Case study:

Statistics of goals of players in each round in Spanish Football League:

Round 1: {'Suarez': 1, 'Messi': 2, 'Benzema': 1,...}

Round 1: {'Suarez': 2,'C Luo': 1, 'Grisman': 2,...}

Round 1: {'Suarez': 1, 'Torres': 2,' Bell ': 1,...}

Count the players who scored in every game in the first N rounds

Solution:

Option 1

from random import randint,simple
simple('abcdefgh',3)
simple('abcdefgh',randint(3,6))
d1={k:randint(1,4) for k in sample('abcdefgh',randint(3,6))}
d1
d2={k:randint(1,4) for k in sample('abcdefgh',randint(3,6))}
d3={k:randint(1,4) for k in sample('abcdefgh',randint(3,6))}
for k in d1:
    if k in d2 and k in d3:
        print(k)
[k for k in d1 if k in d2 and k in d3]
d1=[d1,d2,d3]
[k for k in d1[0] if all(map(lambda d: k in d,d1[1:]))]

Copy code

Solution:

Use the intersection operation step1 of set: use the keys() method of dictionary to get a set of keys step2: use the map function to get the set of keys of each dictionary step3: use the reduce function to get the intersection of keys sets of all dictionaries

s1=d1.keys()
s2=d2.keys()
s1 & s2
from functools import reduce 
reduce(lambda a,b:a*b,range(1,11))
map(dict.keys,d1)
reduce(lambda a,b:a&b map(dict.keys,d1)
Copy code

6. How to keep the dictionary in order

Case study:

A programming competition system is used to count the time of the contestant's programming problem solving. After the contestant's evening competition, the time of the contestant's problem solving is recorded in the dictionary, so that the result can be inquired according to the contestant's name after the competition. {'LiLei': (2,43),'HanMeiMei':(5,52),'jim':(1,39)...} after the competition, the contestant's results need to be printed in order of ranking. How to achieve this?

Solution:

Use OrderedDict in the standard library collection to replace the built-in dictionary Dict with OrderedDict, and then store the scores of competitors in OrderedDict

d={}
from collections import OrderdDict
od = OrderedDict()
od=['c']
od=['b']
od=['a']
od.keys()
players = list('abcdefgh')
from random import shuffle
shuffle(players)
players
od=OrderedDict()
for i, p in enumerate(players,1)
    od[p] = i0
od
def query_by_name(d,name):
    return d[name]
query_by_name(od,'c')

Copy code

7. How to realize the user's history recording function

Discussion questions:

How to achieve the user's history function (n at most)?

Case study:

Many applications have browsing user history functions, such as:

1. The browser can view the recently viewed web page

2. The video player can view the recently played video files

3.Shell can view commands entered by users

Now we have a simple guessing game. How to add history to show the numbers that users have guessed recently?

from random import randint

def guess(n,k):
    if n ==K:
        print('Yes, the number is%d.' %k)
        return True
        
    if n<k:
        print('Guess big, than%d Small.' %k)
        elif n>k:
            print('Guess smaller than%d Big.'%k)
        return False
def main():
    n = randint(1,100)
    i =1
    while True:
        line= input('[%d] Please enter a number:' %d)
        if line.isdight():
            k = int(line)
            i += 1
            if guess(n,k):
                break
            elif line == 'quit':
                break
if __name__=='__main__':
    main()

Copy code

Solution:

Use queue storage history with capacity of n use deque in standard database collections, which is a two terminal queue that uses pickle module to store history to hard disk for next startup

 

 

from collections import deque
deque([],5)
q.append(1)
q.append(2)
q.append(3)
q.append(4)
q.append(5)
q
deque([1,2,3,4,5],maxlen=5)
q.append(6)
q
deque([2,3,4,5,6],maxlen=5)

from random import randint
from collections import deque

def guess(n,k):
    if n ==K:
        print('Yes, the number is%d.' %k)
        return True
        
    if n<k:
        print('Guess big, than%d Small.' %k)
        elif n>k:
            print('Guess smaller than%d Big.'%k)
        return False
def main():
    n = randint(1,100)
    i =1
    hq=deque([],5)
    while True:
        line= input('[%d] Please enter a number:' %d)
        if line.isdight():
            k = int(line)
            hq.append(k)
            i += 1
            if guess(n,k):
                break
            elif line == 'quit':
                break
            elif line=='h?'
                print(list(hq))
                
if __name__=='__main__':
    main()

import pickle
pickle.dump
q
deque([2,3,4,5,6],maxlen=5)
pickle.dump(q,open('save.pkl','wb'))
ed save.pkl
q2=pickle.load(open('save.pk1','rb'))
Copy code

This paper explains the list, the dictionary set according to the condition filter data, according to the dictionary value size sorting, statistics of the frequency of elements in the sequence, how to achieve the user's historical record function.
Finally note: whether you are for Python employment or hobbies, remember: project development experience is always the core. If you don't have the latest Python introduction to the advanced practical video tutorial in 2020, you can go to the small Python exchange : you can find a lot of new Python tutorial projects under the transformation of "seven clothes, nine seven buses and five numbers" (homophony of numbers). You can also communicate with the old driver for advice!

The text and pictures of this article come from the Internet and my own ideas. They are only for learning and communication. They have no commercial use. The copyright belongs to the original author. If you have any questions, please contact us in time for handling.

Tags: Python Lambda Programming shell

Posted on Sun, 26 Apr 2020 23:47:54 -0700 by cmaclennan