Categories
Python Answers

How to generate random numbers with a given (numerical) distribution with Python?

Sometimes, we want to generate random numbers with a given (numerical) distribution with Python.

In this article, we’ll look at how to generate random numbers with a given (numerical) distribution with Python.

How to generate random numbers with a given (numerical) distribution with Python?

To generate random numbers with a given (numerical) distribution with Python, we can use the choice function from the random module.

For instance, we write:

from random import choices

population = [1, 2, 3, 4, 5, 6]
weights = [0.1, 0.05, 0.05, 0.2, 0.4, 0.2]
num = choices(population, weights)
print(num)

We call choices with the population list which we draw numbers from and the weights list which are numerical distribution of each value.

The returned number is assigned to num.

Conclusion

To generate random numbers with a given (numerical) distribution with Python, we can use the choice function from the random module.

Categories
Python Answers

How to convert Python datetime to epoch with strftime?

Sometimes, we want to convert Python datetime to epoch with strftime.

In this article, we’ll look at how to convert Python datetime to epoch with strftime.

How to convert Python datetime to epoch with strftime?

To convert Python datetime to epoch with strftime, we can call the timestamp method.

For instance, we write:

import datetime

t = datetime.datetime(2020, 4, 1, 0, 0).timestamp()
print(t)

We call timestamp on the datetime object and assign the returned timestamp to t.

Therefore, t is 1585699200.0.

Conclusion

To convert Python datetime to epoch with strftime, we can call the timestamp method.

Categories
Python Answers

How to replace multiple characters in a string with Python?

Sometimes, we want to replace multiple characters in a string with Python.

In this article, we’ll look at how to replace multiple characters in a string with Python.

How to replace multiple characters in a string with Python?

To replace multiple characters in a string with Python, we can chain the string replace method.

For instance, we write:

strs = "abc&def#ghi"
s = strs.replace('&', '\&').replace('#', '\#')
print(s)

We call strs.replace with '&' and '\&' to replace & and '\&'.

And then we call it again to replace '#' with '\#'.

Finally, we assign the returned string to s.

Therefore, s is 'abc\&def\#ghi'.

Conclusion

To replace multiple characters in a string with Python, we can chain the string replace method.

Categories
Python Answers

How to create a list of random numbers without duplicates with Python?

Sometimes, we want to create a list of random numbers without duplicates with Python.

In this article, we’ll look at how to create a list of random numbers without duplicates with Python.

How to create a list of random numbers without duplicates with Python?

To create a list of random numbers without duplicates with Python, we can use the random.sample method.

For instance, we write:

import random

l = random.sample(range(100), 10)
print(l)

We call random.sample with the range of numbers to generate and the number of random numbers to generate respectively.

We generate numbers between 0 and 99 with range(100).

And then we assign the returned list to l.

Therefore, l is something like [34, 77, 97, 83, 22, 28, 74, 93, 59, 87].

Conclusion

To create a list of random numbers without duplicates with Python, we can use the random.sample method.

Categories
Python Answers

How to remove elements that have consecutive duplicates with Python?

Sometimes, we want to remove elements that have consecutive duplicates with Python.

In this article, we’ll look at how to remove elements that have consecutive duplicates with Python.

How to remove elements that have consecutive duplicates with Python?

To remove elements that have consecutive duplicates with Python, we can use the groupby function from the itertools module and list comprehension.

For instance, we write:

from itertools import groupby

l = [1, 1, 1, 1, 1, 1, 2, 3, 4, 4, 5, 1, 2]

no_dups = [x[0] for x in groupby(l)]
print(no_dups)

We call groupby with l to group the elements in l by their values and return a new iterable with the grouped items in their own list.

Then we get the first element from each group with x[0] and assign the returned list to no_dups.

Therefore, no_dups is [1, 2, 3, 4, 5, 1, 2].

Conclusion

To remove elements that have consecutive duplicates with Python, we can use the groupby function from the itertools module and list comprehension.