Categories
Python Answers

How to deep copy a list with Python?

Sometimes, we want to deep copy a list with Python.

In this article, we’ll look at how to deep copy a list with Python.

How to deep copy a list with Python?

To deep copy a list with Python, we can use the copy.deepcopy method.

For instance, we write

import copy

a = [[1, 2, 3], [4, 5, 6]]
b = copy.deepcopy(a)

to call copy.deepcopy with a to return a deep copy of a and assign it to b.

Conclusion

To deep copy a list with Python, we can use the copy.deepcopy method.

Categories
Python Answers

How to combine two dicts and add values for keys that appear in both with Python?

Sometimes, we want to combine two dicts and add values for keys that appear in both with Python.

In this article, we’ll look at how to combine two dicts and add values for keys that appear in both with Python.

How to combine two dicts and add values for keys that appear in both with Python?

To combine two dicts and add values for keys that appear in both with Python, we can use the Counter class.

For instance, we write

from collections import Counter

A = Counter({'a':1, 'b':2, 'c':3})
B = Counter({'b':3, 'c':4, 'd':5})
C = A + B

to create Counter objects from the dicts.

Then we combine them into one and add the values with the given keys together with +.

Conclusion

To combine two dicts and add values for keys that appear in both with Python, we can use the Counter class.

Categories
Python Answers

How to plot in real-time in a while loop using Python matplotlib?

Sometimes, we want to plot in real-time in a while loop using Python matplotlib.

In this article, we’ll look at how to plot in real-time in a while loop using Python matplotlib.

How to plot in real-time in a while loop using Python matplotlib?

To plot in real-time in a while loop using Python matplotlib, we can create a loop to plot the data and then call pause.

For instance, we write

import numpy as np
import matplotlib.pyplot as plt

plt.axis([0, 10, 0, 1])

for i in range(10):
    y = np.random.random()
    plt.scatter(i, y)
    plt.pause(0.05)

plt.show()

to call scatter to plot a scatterplot.

Then we call pause to draw the new data and run the GUI’s `event loop.

And then we call show to show the GUI.

Conclusion

To plot in real-time in a while loop using Python matplotlib, we can create a loop to plot the data and then call pause.

Categories
Python Answers

How to fix Python multiprocessing PicklingError: Can’t pickle function error?

Sometimes, we want to fix Python multiprocessing PicklingError: Can’t pickle function error.

In this article, we’ll look at how to fix Python multiprocessing PicklingError: Can’t pickle function error.

How to fix Python multiprocessing PicklingError: Can’t pickle function error?

To fix Python multiprocessing PicklingError: Can’t pickle function error, we should only run pickle in top level functions.

For instance, we write

import multiprocessing as mp

# ...

def work(foo):
    foo.work()

if __name__ == '__main__':   
    pool = mp.Pool()
    pool.apply_async(work, args=(foo,))
    pool.close()
    pool.join()

to define the work top level function,

And then we call pool.apply_async with work and the args arguments to run work which runs foo.work, which runs the pickle code.

Conclusion

To fix Python multiprocessing PicklingError: Can’t pickle function error, we should only pickle top level functions.

Categories
Python Answers

How to use global variables between files with Python?

Sometimes, we want to use global variables between files with Python.

In this article, we’ll look at how to use global variables between files with Python.

How to use global variables between files with Python?

To use global variables between files with Python, we can import the variable in another module.

For instance, we write

settings.py

def init():
    global my_list
    my_list = []

to add the my_list global variable.

Then we import the variable in foo.py and use it by writing

import settings

def stuff():
    settings.my_list.append('abc')

to import the global variables from settings.py with

import settings

And then we use it by calling settings.my_list.append.

Conclusion

To use global variables between files with Python, we can import the variable in another module.