Categories
Python Answers

How to get list of values for list of keys with Python dictionaries?

Sometimes, we want to get list of values for list of keys with Python dictionaries.

In this article, we’ll look at how to get list of values for list of keys with Python dictionaries.

How to get list of values for list of keys with Python dictionaries?

To get list of values for list of keys with Python dictionaries, we can use list comprehension.

For instance, we write

l = [mydict[x] for x in mykeys]

to get the keys from mykeys and get the values of each key x in the mydict dict with mydict[x].

And then we put the values in a list.

Conclusion

To get list of values for list of keys with Python dictionaries, we can use list comprehension.

Categories
Python Answers

How to get href with Python BeautifulSoup?

Sometimes, we want to get href with Python BeautifulSoup.

In this article, we’ll look at how to get href with Python BeautifulSoup.

How to get href with Python BeautifulSoup?

To get href with Python BeautifulSoup, we can use the find_all method.

For instance, we write

from BeautifulSoup import BeautifulSoup

html = '''<a href="some_url">next</a>
<span class="class"><a href="another_url">later</a></span>'''

soup = BeautifulSoup(html)

for a in soup.find_all('a', href=True):
    print(a['href'])

to create soup object with BeautifulSoup class called with the html string.

Then we find the a elements with the href attribute returned by calling find_all with 'a' and href set to True.

Then we print out the href attribute values of the a elements in the loop.

Conclusion

To get href with Python BeautifulSoup, we can use the find_all method.

Categories
Python Answers

How to specify new lines on Python, when writing on files?

Sometimes, we want to specify new lines on Python, when writing on files.

In this article, we’ll look at how to specify new lines on Python, when writing on files.

How to specify new lines on Python, when writing on files?

To specify new lines on Python, when writing on files, we can write the new line characters separately.

For instance, we write

file.write(line1)
file.write("\n")
file.write(line2)
file.write("\n")
file.write(line3)
file.write("\n")

to call file.write with the line1, line2, and line3 strings separated by newline characters.

file is the file opened with open with write permissions.

Conclusion

To specify new lines on Python, when writing on files, we can write the new line characters separately.

Categories
Python Answers

How to make sure only a single instance of a program is running with Python?

Sometimes, we want to make sure only a single instance of a program is running with Python.

In this article, we’ll look at how to make sure only a single instance of a program is running with Python.

How to make sure only a single instance of a program is running with Python?

To make sure only a single instance of a program is running with Python, we use the tendo module.

We install it by running

pip install tendo

Then we write

from tendo import singleton

me = singleton.SingleInstance()

to create the singleton.SingleInstance object to exit the program with code -1 is other instances of the program is running.

Conclusion

To make sure only a single instance of a program is running with Python, we use the tendo module.

Categories
Python Answers

How to plot in a non-blocking way with Python Matplotlib?

Sometimes, we want to plot in a non-blocking way with Python Matplotlib.

In this article, we’ll look at how to plot in a non-blocking way with Python Matplotlib.

How to plot in a non-blocking way with Python Matplotlib?

To plot in a non-blocking way with Python Matplotlib, we can use the draw method.

For instance, we write

import numpy as np
from matplotlib import pyplot as plt

def main():
    plt.axis([-50,50,0,10000])
    plt.ion()
    plt.show()

    x = np.arange(-50, 51)
    for pow in range(1, 5):
        y = [n**pow for n in x]
        plt.plot(x, y)
        plt.draw()
        plt.pause(0.001)
        input("Press [enter] to continue.")

if __name__ == '__main__':
    main()

to create the NumPy array x with the values for the x-axis.

Then we create y with the values in x raised to the pow power to create the values for the y-axis.

And then we call plot to plot with x and y.

Next, we call draw to draw the plot in a non-blocking way.

And then we call pause to pause plotting until enter is pressed.

Conclusion

To plot in a non-blocking way with Python Matplotlib, we can use the draw method.