Categories
Python Answers

How to filter a dictionary according to an arbitrary condition function with Python?

Sometimes, we want to filter a dictionary according to an arbitrary condition function with Python.

In this article, we’ll look at how to filter a dictionary according to an arbitrary condition function with Python.

How to filter a dictionary according to an arbitrary condition function with Python?

To filter a dictionary according to an arbitrary condition function with Python, we can use dict comprehension.

For instance, we write

d = {k: v for k, v in points.items() if v[0] < 5 and v[1] < 5}

to get the keys and values from the points dict with items.

Then we filter the values v with if v[0] < 5 and v[1] < 5.

And we return the filter keys and values with k: v.

Conclusion

To filter a dictionary according to an arbitrary condition function with Python, we can use dict comprehension.

Categories
Python Answers

How to implement an ordered, default dict with Python?

Sometimes, we want to implement an ordered, default dict with Python.

In this article, we’ll look at how to implement an ordered, default dict with Python.

How to implement an ordered, default dict with Python?

To implement an ordered, default dict with Python, we can use the OrderedDict.fromkeys method.

For instance, we write

from collections import OrderedDict

keys = ['a', 'b', 'c']
od = OrderedDict.fromkeys(keys)

to call OrderedDict.fromkeys with the keys list to returned an ordered dict with the keys being the values in the keys list.

And the values are all set to None.

Conclusion

To implement an ordered, default dict with Python, we can use the OrderedDict.fromkeys method.

Categories
Python Answers

How to plot a smooth line with PyPlot and Python?

Sometimes, we want to plot a smooth line with PyPlot and Python.

In this article, we’ll look at how to plot a smooth line with PyPlot and Python.

How to plot a smooth line with PyPlot and Python?

To plot a smooth line with PyPlot and Python, we can use the spline function.

For instance, we write

from scipy.interpolate import spline

xnew = np.linspace(T.min(), T.max(), 300)  

power_smooth = spline(T, power, xnew)

plt.plot(xnew, power_smooth)
plt.show()

to call splint with 300 to let us plot 300 points between T.min() and T.max().

Then we call plot with xnew and power_smooth to plot a smooth plot.

Conclusion

To plot a smooth line with PyPlot and Python, we can use the spline function.

Categories
Python Answers

How to change the default Python version?

Sometimes, we want to change the default Python version.

In this article, we’ll look at how to change the default Python version.

How to change the default Python version?

To change the default Python version, we can add the alias for python in the bash_profile file in the home directory.

We run

which python3

to get the directory of python3.

Then we open bash_profile and add the alias for python with

alias python='/usr/local/bin/python3'

Then we reload bash profile with

source ~/.bash_profile

Then we check the Python version with

python --version

Conclusion

To change the default Python version, we can add the alias for python in the bash_profile file in the home directory.

Categories
Python Answers

How to sort two lists which reference each other in the exact same way with Python?

Sometimes, we want to sort two lists which reference each other in the exact same way with Python.

In this article, we’ll look at how to sort two lists which reference each other in the exact same way with Python.

How to sort two lists which reference each other in the exact same way with Python?

To sort two lists which reference each other in the exact same way with Python, we can use the zip and sorted functions.

For instance, we write

list1, list2 = (list(t) for t in zip(*sorted(zip(list1, list2))))

to call zip with with 2 lists to create a list with tuples with items in the 2 lists at the same position.

Then we call sorted to sort the items in the tuples list.

Next, we call zip again with the sorted tuples as the argument to create a tuple with 2 tuples with the items extracted from the tuples at each position and put into a tuple.

Then we unpack the lists by assigning them back to list1 and list2.

Conclusion

To sort two lists which reference each other in the exact same way with Python, we can use the zip and sorted functions.