程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

13 very useful Python code snippets, recommended collection!

編輯:Python

author  |  Zhou radish

source |  Radish chowder

Today, we will mainly introduce the common in the application Python code snippet , Let's make progress together.

Lists Snippets

Let's start with a list of the most commonly used data structures

№1: Merge two lists into one dictionary

Suppose we were Python There are two lists in , We want to merge them into a dictionary , One of the items in the list acts as the key of the dictionary , The other is the value . It's in use Python A very common problem when writing code

But to solve this problem , We need to consider several limitations , For example, the size of two lists , Types of elements in both lists , And whether there are repeated elements in it , In particular, we use the element as key when . We can do that by using zip And other built-in functions to solve these problems

keys_list = ['A', 'B', 'C']
values_list = ['blue', 'red', 'bold']
#There are 3 ways to convert these two lists into a dictionary
#1- Using Python's zip, dict functionz
dict_method_1 = dict(zip(keys_list, values_list))
#2- Using the zip function with dictionary comprehensions
dict_method_2 = {key:value for key, value in zip(keys_list, values_list)}
#3- Using the zip function with a loop
items_tuples = zip(keys_list, values_list) 
dict_method_3 = {} 
for key, value in items_tuples: 
    if key in dict_method_3: 
        pass # To avoid repeating keys.
    else: 
        dict_method_3[key] = value

№2: Merge two or more lists into a single list containing lists

Another common task is when we have two or more lists , We want to collect them all in a big list , All the first items of the smaller list form the first list in the larger list

for example , If we had 4 A list [1,2,3], ['a','b','c'], ['h','e','y'] and [4,5, 6], We want to create a new list for these four lists ; It will be [[1,'a','h',4], [2,'b','e',5], [3,'c','y',6]]

def merge(*args, missing_val = None):
#missing_val will be used when one of the smaller lists is shorter tham the others.
#Get the maximum length within the smaller lists.
  max_length = max([len(lst) for lst in args])
  outList = []
  for i in range(max_length):
    result.append([args[k][i] if i < len(args[k]) else missing_val for k in range(len(args))])
  return outList

№3: Sort the dictionary list

This set of daily list tasks is the sorting task , According to the data type of the elements contained in the list , We'll sort them in a slightly different way .

dicts_lists = [
  {
    "Name": "James",
    "Age": 20,
  },
  {
     "Name": "May",
     "Age": 14,
  },
  {
    "Name": "Katy",
    "Age": 23,
  }
]
#There are different ways to sort that list
#1- Using the sort/ sorted function based on the age
dicts_lists.sort(key=lambda item: item.get("Age"))
#2- Using itemgetter module based on name
from operator import itemgetter
f = itemgetter('Name')
dicts_lists.sort(key=f)

№4: Sort the string list

We often face lists containing strings , We need to be alphabetical 、 Sort these lists by length or any other factor we want or need for our application

my_list = ["blue", "red", "green"]
#1- Using sort or srted directly or with specifc keys
my_list.sort() #sorts alphabetically or in an ascending order for numeric data 
my_list = sorted(my_list, key=len) #sorts the list based on the length of the strings from shortest to longest. 
# You can use reverse=True to flip the order
#2- Using locale and functools 
import locale
from functools import cmp_to_key
my_list = sorted(my_list, key=cmp_to_key(locale.strcoll))

№5: Sort the list according to another list

Sometimes , We may need to use one list to sort another list , therefore , We will have a list of numbers ( Indexes ) And a list that we want to sort using these indexes

a = ['blue', 'green', 'orange', 'purple', 'yellow']
b = [3, 2, 5, 4, 1]
#Use list comprehensions to sort these lists
sortedList =  [val for (_, val) in sorted(zip(b, a), key=lambda x: \
          x[0])]

№6: Map list to dictionary

The last task of listing code snippets , If you give a list and map it to a dictionary , in other words , We want to convert our list into a dictionary with numeric keys

mylist = ['blue', 'orange', 'green']
#Map the list into a dict using the map, zip and dict functions
mapped_dict = dict(zip(itr, map(fn, itr)))

Dictionary Snippets

The data type being processed now is the dictionary

№7: Merge two or more dictionaries

Suppose we have two or more dictionaries , And we want to merge them all into a dictionary with unique keys

from collections import defaultdict
#merge two or more dicts using the collections module
def merge_dicts(*dicts):
  mdict = defaultdict(list)
  for dict in dicts:
    for key in dict:
      res[key].append(d[key])
  return dict(mdict)

№8: Reverse the dictionary

A very common dictionary task is if we have a dictionary and want to flip its keys and values , The key will become the value , And the value will become the key

When we do this , We need to make sure there are no duplicate keys . Values can be repeated , But the key cannot , And make sure all new keys are OK hashable Of

my_dict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
#Invert the dictionary based on its content
#1- If we know all values are unique.
my_inverted_dict = dict(map(reversed, my_dict.items()))
#2- If non-unique values exist
from collections import defaultdict
my_inverted_dict = defaultdict(list)
{my_inverted_dict[v].append(k) for k, v in my_dict.items()}
#3- If any of the values are not hashable
my_dict = {value: key for key in my_inverted_dict for value in my_inverted_dict[key]}

String Snippets

The next step is string processing

№9: Use f character string

Formatting strings can be a task we need to do almost every day , stay Python There are several ways to format strings in , Use f String is a good choice

#Formatting strings with f string.
str_val = 'books'
num_val = 15
print(f'{num_val} {str_val}') # 15 books
print(f'{num_val % 2 = }') # 1
print(f'{str_val!r}') # books
#Dealing with floats
price_val = 5.18362
print(f'{price_val:.2f}') # 5.18
#Formatting dates
from datetime import datetime;
date_val = datetime.utcnow()
print(f'{date_val=:%Y-%m-%d}') # date_val=2021-09-24

№10: Check substring

A very common task is to check whether the string is in the and string list

addresses = ["123 Elm Street", "531 Oak Street", "678 Maple Street"]
street = "Elm Street"
#The top 2 methods to check if street in any of the items in the addresses list
#1- Using the find method
for address in addresses:
    if address.find(street) >= 0:
        print(address)
#2- Using the "in" keyword 
for address in addresses:
    if street in address:
        print(address)

№11: Gets the size of the string in bytes

Sometimes , Especially when building memory critical applications , We need to know how much memory our string uses

str1 = "hello"
str2 = ""
def str_size(s):
  return len(s.encode('utf-8'))
str_size(str1)
str_size(str2)

Input/ Output operations

Finally, let's look at the input and output code snippets

№12: Check if the file exists

In data science and many other applications , We often need to read data from or write data to files , But to do that , We need to check if the file exists , therefore , We need to make sure that the code doesn't IO To terminate by mistake

#Checking if a file exists in two ways
#1- Using the OS module
import os 
exists = os.path.isfile('/path/to/file')
#2- Use the pathlib module for a better performance
from pathlib import Path
config = Path('/path/to/file') 
if config.is_file(): 
    pass

№13: Parse spreadsheet

Another very common file interaction is parsing data from spreadsheets , We use CSV Module to help us perform the task effectively

import csv
csv_mapping_list = []
with open("/path/to/data.csv") as my_data:
    csv_reader = csv.reader(my_data, delimiter=",")
    line_count = 0
    for line in csv_reader:
        if line_count == 0:
            header = line
        else:
            row_dict = {key: value for key, value in zip(header, line)}
            csv_mapping_list.append(row_dict)
        line_count += 1

Okay , We studied together 13 Code snippets , These clips are simple 、 Short and efficient , No matter what application area we work in , Will eventually be in the corresponding Python Use at least one of them in the project , So collection is the best choice !

Looking back

NLP Exploration and practice of class problem modeling scheme

Python Common encryption algorithms in crawlers !

2D Transformation 3D, Look at NVIDIA's AI“ new ” magic !

Get it done Python Several common data structures !

 Share
Point collection
A little bit of praise
Click to see 

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved