Today, we will mainly introduce the common in the application Python code snippet , Let's make progress together.
Let's start with a list of the most commonly used data structures
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
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
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)
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))
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])]
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)))
The data type being processed now is the dictionary
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)
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]}
The next step is string processing
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
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)
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)
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
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 !
Okay , Today's sharing is here , Just order one if you like Fabulous Well !
Original address :https://towardsdatascience.com/13-useful-python-snippets-that-you-need-to-know-91580af9b1f6
- END -
contrast Excel The cumulative sales of the series of books reached 15w book , Make it easy for you to master data analysis skills , You can click on the link below to learn about purchasing :