current position:Home>15 advanced Python tips for experienced programmers

15 advanced Python tips for experienced programmers

2022-01-30 11:23:30 Yunyun yyds

This article will introduce 15 A concise Python skill , Towards simplicity and efficiency , Learning is easy to understand .  Insert picture description here

1. Sort objects by multiple key values

Suppose you want to sort the following dictionary list :

people = [ 
{ 'name': 'John', "age": 64 }, 
{ 'name': 'Janet', "age": 34 }, 
{ 'name': 'Ed', "age": 24 }, 
{ 'name': 'Sara', "age": 64 }, 
{ 'name': 'John', "age": 32 }, 
{ 'name': 'Jane', "age": 34 }, 
{ 'name': 'John', "age": 99 }, 
]
 Copy code 

Not just by name or age , Also sort the two fields at the same time . stay SQL in , It will be such a query :

SELECT * FROM people ORDER by name, age
 Copy code 

actually , The solution to this problem can be very simple ,Python Guarantee sort Function provides a stable sort order , This also means that more similar items will retain their original order . To achieve sorting by name and age , You can do this :

import operator 
people.sort(key=operator.itemgetter('age')) 
people.sort(key=operator.itemgetter('name'))
 Copy code 

Notice how to reverse the order . First, by age , Then sort by name , Use operator.itemgetter() Get the age and name fields from each dictionary in the list , So you'll get the results you want :

[ 
{'name': 'Ed', 'age': 24}, 
{'name': 'Jane', 'age': 34}, 
{'name': 'Janet','age': 34}, 
{'name': 'John', 'age': 32}, 
{'name': 'John', 'age': 64}, 
{'name': 'John', 'age': 99}, 
{'name': 'Sara', 'age': 64} 
]
 Copy code 

The name is the main sort item , If the names are the same , Sort by age . therefore , all John All grouped by age .

2. Data categories

since 3.7 After version ,Python Start providing data categories . Compared with conventional classes or other alternatives ( For example, multiple values or dictionaries are returned ), It has more advantages :

  1. Data classes require very little code
  2. You can compare data classes , because eq This function can be realized
  3. Data class needs type prompt , Reduces the possibility of errors
  4. You can easily print data classes for debugging , because __repr__ This function can be realized

This is an example of a working data class :

from dataclasses import dataclass 
 @dataclass
     classCard: 
      rank: str
      suit: str
      card=Card("Q", "hearts") 
     print(card == card) 
     # True 
     print(card.rank) 
     # 'Q' 
     print(card) 
     Card(rank='Q', suit='hearts')
 Copy code 

3. The list of deduction

List derivation can replace annoying loops in list filling , Its basic grammar is

[ expression for item in list if conditional ]
 Copy code 

Let's look at a very basic example , Fill the list with a sequence of numbers :

mylist = [i for i inrange(10)] 
    print(mylist) 
    # [0, 1, 2, 3,4, 5, 6, 7, 8, 9]
 Copy code 

Because you can use expressions , So you can also do some mathematical operations :

squares = [x**2for x inrange(10)] 
    print(squares) 
    # [0, 1, 4, 9,16, 25, 36, 49, 64, 81]
 Copy code 

You can even call external functions :

defsome_function(a): 
        return (a +5) /2
         
       my_formula= [some_function(i) for i inrange(10)] 
       print(my_formula) 
       # [2.5, 3.0,3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0]
 Copy code 

Last , have access to if Function to filter the list . under these circumstances , Only reserved for 2 Value of division :

filtered = [i for i inrange(20) if i%2==0] 
    print(filtered) 
    # [0, 2, 4, 6,8, 10, 12, 14, 16, 18]
 Copy code 

4. Check the memory usage of the object

Use sys.getsizeof() You can check the memory usage of objects :

import sys 
     mylist =range(0, 10000) 
   print(sys.getsizeof(mylist)) 
   # 48
 Copy code 

Why is this huge list only 48 Bytes? , This is because range The class returned by the function is represented as a list . Compared to using an actual list of numbers , The storage efficiency of number sequence is much higher . We can use list derivation to create a list of actual numbers in the same range :

import sys 
     myreallist = [x for x inrange(0, 10000)] 
   print(sys.getsizeof(myreallist)) 
   # 87632
 Copy code 

By using sys.getsizeof(), We can learn more about Python And memory usage .

5. Find the most frequent values

To find the most frequent value in a list or string :

test = [1, 2, 3, 4, 2, 2, 3, 1, 4, 4, 4] 
  print(max(set(test), key = test.count)) 
  # 4
 Copy code 
  • max() The maximum value in the list will be returned .key Parameter uses a single parameter function to customize the sorting order , In this case test.count, This function applies to each item on the iterator .
  • test.count yes list Built in features of . It takes a parameter , And calculate the occurrence times of this parameter . therefore test.count(1) Will return 2, and test.count(4) Will return 4.
  • set(test) return test All unique values in , therefore {1、2、3、4}

Then in this line of code will accept test All unique values of , namely {1、2、3、4}. Next ,max It will be applied list.count Function and returns the maximum value .

There is a more effective way :

from collections import Counter 
Counter(test).most_common(1) 
# [4: 4]
 Copy code 

6. Property package

You can use attrs Instead of data classes , choice attrs There are two reasons :

  1. The use of Python Version higher than 3.7
  2. Want more features

Theattrs The software package supports all mainstream applications Python edition , Include CPython 2.7 and PyPy. some attrs You can provide unconventional data classes such as validators and Converters . Let's look at some sample code :

@attrs
   classPerson(object): 
    name =attrib(default='John') 
    surname =attrib(default='Doe') 
    age =attrib(init=False) 
    p =Person() 
   print(p) 
   p=Person('Bill', 'Gates') 
   p.age=60
   print(p) 
     # Output: 
   # Person(name='John', surname='Doe',age=NOTHING) 
   # Person(name='Bill', surname='Gates', age=60)
 Copy code 

actually ,attrs The authors of are already using the method of introducing data classes PEP 了 . Data classes are deliberately kept simpler 、 Easier to understand , and attrs Provides all the features you may need .

7. Merge dictionaries (Python3.5+)

dict1 = { 'a': 1, 'b': 2 } 
  dict2= { 'b': 3, 'c': 4 } 
  merged= { **dict1, **dict2 } 
  print (merged) 
  # {'a': 1, 'b':3, 'c': 4}
 Copy code 

If there are overlapping keys , The keys in the first dictionary will be overwritten . stay Python 3.9 in , Merging dictionaries becomes more concise . above Python 3.9 The merge in can be rewritten as :

merged = dict1 | dict2
 Copy code 

8. Return multiple values

Python The function in does not have a dictionary , In the case of lists and classes, you can return multiple variables , It works as follows :

defget_user(id): 
      # fetch user from database 
      # .... 
      return name, birthdate 
     name, birthdate =get_user(4)
 Copy code 

This is a finite return value , But anything more than 3 The contents of each value should be placed in a ( data ) class .

9. Filtering of list elements

filter() Use

filter() Function acceptance 2 Parameters :

  1. Function object
  2. Objects that can be iterated

Next we define 1 Functions, and then to 1 List to filter .

First we create 1 A list , And eliminate less than or equal to 3 The elements of :

original_list = [ 1,2,3,4,5]# Definition list 
  
# Define filter functions 
4 def filter_three(number):5
  return number > 3
    
filtered = filter(filter_three, original_list)
filtered_list = list(filtered)
filtered_list
  
#[4,5]
 Copy code 

We define lists original_list Then we define a parameter that accepts a numerical type number Function of filter_three, When the value of the parameter passed in is greater than 3 Will return to True, On the contrary, it will return False We defined filter object filtered, among filter() The first argument accepted is the function object , The second parameter is the list object, and eventually we will filter Object to list , In the end, I got the experience filter_three After filtration original_list The elements left in .

Allied , We can also use list derivation to filter list elements , As an elegant way to generate and modify lists , Here's how to do the same thing using list derivation :

original_list = [1,2,3,4,5]2
filtered_list = [ number for number in original_list if number > 3]# In the process of list derivation, conditional judgment is introduced 
print(filtered_list)
  
#[4,5]
 Copy code 

10. Modify the list

map() Use

Python The built-in map() Functions allow us to apply a function to every element in an iteratable object .

For example, we want to get the square of every element in a list object , You can use map() function , Like the following example :

original_list = [1,2,3,4,5]
def square( number):
    return number **2
squares =map(square, original_list)
squares_list = list( squares)
print(squares_list) 
  
#[1,4,9,16,25]
 Copy code 

similar filter() Working process of , Now let's see what happened :

First, we define lists original_list, And a function that takes a numeric parameter and returns its square value square() And then we define map object squares, similar filter(),map() The first argument accepted is the function object , The second parameter is the list object, and eventually we will map object squares List , You get the result you want .

In the same way, we can do the same thing with list derivation :

original_list = [1,2,3,4,5]
squares_list = [number ** 2for number in original_list]
print(squares_list)
  
#[1,4,9, 16,25]
 Copy code 

11. utilize zip() To combine lists

In some cases we need to combine two or more lists together , This kind of requirement uses zip() It's very convenient to do it . zip() Function receives multiple lists as arguments , And then we get the one-to-one combination of elements in each position , Like the following example :

numbers = [ 1,2,3]
letters = [ 'a', 'b', 'c']
combined = zip(numbers,letters)
combined_list = list( combined)
print(combined_list)
for item in zip( numbers,letters ):
    print(item[0], '\t', item[1])
      
#[(1,'a'),(2,'b'),(3, 'c')]
#1 a
#2 b
#3 c
 Copy code 

12. Reverse the list

Python The list in is an ordered data structure , Because of this , The order of the elements in the list is important , Sometimes we need to flip the order of all the elements in the list , Can pass Python Slice operations in , use ::-1 To quickly implement :

original_list = [1,2,3,4,5]

reversed_list = original_list[ : : -1]

print(' Before turning over : ', original_list)
print(' After flipping :', reversed_list)
  
# Before turning over :[ 1,2,3,4,5]
# After flipping :[5,4,3,2,1]
 Copy code 

13. Check the existence of elements in the list

In some cases, we want to check whether there is an element in the list , You can use it at this time Python Medium in Operator , For example, we have a list of the names of all the winning teams in the game , When we want to find out if a team name has won , It can be like the following example :

games = [ 'Yankees ', 'Yankees ', 'Cubs ', 'Blue Jays ', 'Giants ']
def isin(item,list_name) :
    if item in list_name: print(f"{item} is in the list! ")
    else: print(f"{item} is not in the list! ")

isin( 'Blue Jays ' , games)
isin( ' Angels', games)

#Blue Jays is in the list!
#Angels is not in the list!
 Copy code 

14. Flatten nested lists

In some cases we come across nested lists , Each element is a different list , In this case, we can use the list derivation to flatten the nested list , As below 2 An example of nesting layers :

nested_list = [[1,2,3],[4,5,6],[7,8,9]]

flat_list = [i for j in nested_list for i in j]

print(flat_list)

#[1,2,3,4,5,6,7,8,9]
 Copy code 

Add extra :

Only two nested lists are considered here , If it's more layers nested , You need to write as many layers as you need for loop , More trouble , In fact, there is a better way , We can use pip install dm-tree To install tree This library is dedicated to flattening nested structures , You can flatten nested lists at any level , Examples are as follows :

import tree

nested_list_2d = [[1,2,3],[4,5,6],[7,8,9]]

nested_list_3d = [[[1,2],[3,4]],
                          [[5,6],[7,8]],
                          [[9,10],[11,12]]]
                           
print(tree.flatten(nested_list_2d))
print(tree.flatten(nested_list_3d))

#[1,2,3,4,5,6,7,8,9]
#[1,2,3,4,5,6,7,,8, 9, 10, 11,12]
 Copy code 

15. Check uniqueness

If you want to see if all the values in the list are unique , have access to Python Medium set The characteristics of data structure , For example, the following example :

list1 = [ 1,2,3,4,5]
list2 = [1,1,2,3,4]

def isunique( 1):
    if len(l) == len(set(l)) :
       print(  only ! ') eise: print((' Not only —! ') isunique( list1) isunique(list2) # only —! # Not only —!  Copy code 

Look forward to your third company ( give the thumbs-up , Collection , Comment on ), Your support is the driving force of my continuous output , thank .  Insert picture description here

① Interesting source code and learning experience ② Tool installation package , Install video ③ More convenient than books to read anytime, anywhere 300 This ebook ④ Answer questions professionally

Click to collect

copyright notice
author[Yunyun yyds],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201301123281541.html

Random recommended