Categories
Python Answers

How to get a random value from dictionary with Python?

Sometimes, we want to get a random value from dictionary with Python.

In this article, we’ll look at how to get a random value from dictionary with Python.

How to get a random value from dictionary with Python?

To get a random value from dictionary with Python, we can use the random.choice method with the dictionary’s values method.

For instance, we write:

import random

d = {'VENEZUELA': 'CARACAS', 'CANADA': 'OTTAWA'}
c = random.choice(list(d.values()))
print(c)

We have the dictionary d which we want to get a random choice from.

Then we call d.values to return a generator with the dictionary’s values.

Next we convert to a list with list.

And then we pick a random choice from the list with random.choice.

Conclusion

To get a random value from dictionary with Python, we can use the random.choice method with the dictionary’s values method.

Categories
Python Answers

How to combine several images horizontally with Python?

Sometimes, we want to combine several images horizontally with Python.

In this article, we’ll look at how to combine several images horizontally with Python.

How to combine several images horizontally with Python?

To combine several images horizontally with Python, we can use the PIL module.

For instance, we write:

from PIL import Image

images = [Image.open(x) for x in ['test1.png', 'test2.jpg']]
widths, heights = zip(*(i.size for i in images))

total_width = sum(widths)
max_height = max(heights)

new_im = Image.new('RGB', (total_width, max_height))

x_offset = 0
for im in images:
    new_im.paste(im, (x_offset, 0))
    x_offset += im.size[0]

new_im.save('test.jpg')

We open all the images with:

images = [Image.open(x) for x in ['test1.png', 'test2.jpg']]

Then we get widths and heights of all the images and put them in lists with:

widths, heights = zip(*(i.size for i in images))

Then we get the total width and max height with:

total_width = sum(widths)
max_height = max(heights)

which we set as the dimensions of the combined image.

Next, we combine the pixels from both images into a new image with:

new_im = Image.new('RGB', (total_width, max_height))

x_offset = 0
for im in images:
    new_im.paste(im, (x_offset, 0))
    x_offset += im.size[0]

The pixels are pasted with:

new_im.paste(im, (x_offset, 0))

Finally, we save the image with new_im.save('test.jpg').

Conclusion

To combine several images horizontally with Python, we can use the PIL module.

Categories
Python Answers

How to convert a date string to date object with Python?

Sometimes, we want to convert a date string to date object with Python.

In this article, we’ll look at how to convert a date string to date object with Python.

How to convert a date string to date object with Python?

To convert a date string to date object with Python, we can use the datetime.datetime.strptime method.

For instance, we write:

import datetime

d = datetime.datetime.strptime('24052010', "%d%m%Y").date()
print(d)

to convert the '24052010' string into a date object.

We parse the string by passing in "%d%m%Y" as the format string.

%d is the 2 digit date of the month.

%m is the 2 digit month.

And %Y is the 4 digit year.

Then we call date to return the date from the date time object.

Therefore, d is 2010-05-24.

Conclusion

To convert a date string to date object with Python, we can use the datetime.datetime.strptime method.

Categories
Python Answers

How to check if a string contains an element from a list in Python?

Sometimes, we want to check if a string contains an element from a list in Python.

In this article, we’ll look at how to check if a string contains an element from a list in Python.

How to check if a string contains an element from a list in Python?

To check if a string contains an element from a list in Python, we can use the any function.

For instance, we write:

extensions_to_check = ['.txt', '.csv']
url_string = 'test.txt'
if any(ext in url_string for ext in extensions_to_check):
    print(url_string)

We use ext in url_string for ext in extensions_to_check to check if the url_string includes the string is in the extensions_to_check list.

And we use the returned generator with any to do the check.

Since this is True, we see 'test.txt' printed.

Conclusion

To check if a string contains an element from a list in Python, we can use the any function.

Categories
Python Answers

How to access the ith column of a Python NumPy multidimensional array?

Sometimes, we want to access the ith column of a Python NumPy multidimensional array.

In this article, we’ll look at how to access the ith column of a Python NumPy multidimensional array.

How to access the ith column of a Python NumPy multidimensional array?

To access the ith column of a Python NumPy multidimensional array, we can put the column index after :, in the square brackets.

For instance, we write:

import numpy

test = numpy.array([[1, 2], [3, 4], [5, 6]])
col = test[:, 1]
print(col)

We create the test NumPy array with a nested list.

Then we get the 2nd column of the NumPy array with test[:, 1] and assign the returned array to col.

Therefore, col is [2 4 6].

Conclusion

To access the ith column of a Python NumPy multidimensional array, we can put the column index after :, in the square brackets.