Categories
Python Answers

How to create a linked list with Python?

Sometimes, we want to create a linked list with Python.

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

How to create a linked list with Python?

To create a linked list with Python, we can use a deque.

For instance, we write:

from collections import deque

d = deque([1, 2, 3, 4])

print(d)
for x in d:
    print(x)
print(d.pop(), d)

We create a deque with deque([1, 2, 3, 4]).

Next, we loop through the items in d and print them.

Then we call d.pop to remove the last entry from the deque and print the new value of d.

Therefore, we see:

deque([1, 2, 3, 4])
1
2
3
4
4 deque([1, 2, 3])

printed.

Conclusion

To create a linked list with Python, we can use a deque.

Categories
Python Answers

How to execute cat subprocess in parallel with Python?

Sometimes, we want to execute cat subprocess in parallel with Python.

In this article, we’ll look at how to execute cat subprocess in parallel with Python.

How to execute cat subprocess in parallel with Python?

To execute cat subprocess in parallel with Python, we can use the subprocess module.

For instance, we write:

from subprocess import Popen

processes = [
    Popen("echo {i:d}; sleep 2; echo {i:d}".format(i=i), shell=True)
    for i in range(5)
]
exitcodes = [p.wait() for p in processes]

We call Popen with the command we want to run`.

And we set shell to True to let us use the shell.

We specify that we run the commands 5 times with for i in range(5).

Then we return exit codes for each process with [p.wait() for p in processes].

Conclusion

To execute cat subprocess in parallel with Python, we can use the subprocess module.

Categories
Python Answers

How to truncate float values with Python?

Sometimes, we want to truncate float values with Python.

In this article, we’ll look at how to truncate float values with Python.

How to truncate float values with Python?

To truncate float values with Python, we can use the round function.

For instance, we write:

n = round(1.923328437452, 3)
print(n)

to call round with the number to round and the number of decimal places to round to respectively.

And then we assign the rounded number to n.

Therefore, n is 1.923.

Conclusion

To truncate float values with Python, we can use the round function.

Categories
Python Answers

How to insert a list into a cell with Python Pandas?

Sometimes, we want to insert a list into a cell with Python Pandas.

In this article, we’ll look at how to insert a list into a cell with Python Pandas.

How to insert a list into a cell with Python Pandas?

To insert a list into a cell with Python Pandas, we can assign the list we want to insert to the location in the data frame we want to insert it in.

For instance, we write:

import pandas as pd

df = pd.DataFrame(data={'A': [1, 2, 3], 'B': ['x', 'y', 'z']})

df.at[1, 'B'] = ['c', 'd']

We create a data frame by using the DataFrame class with a dictionary.

Then we assign the list ['c', 'd'] into the location [1, 'B'] with:

df.at[1, 'B'] = ['c', 'd']

Therefore, df is now:

   A       B
0  1       x
1  2  [c, d]
2  3       z

Conclusion

To insert a list into a cell with Python Pandas, we can assign the list we want to insert to the location in the data frame we want to insert it in.

Categories
Python Answers

How to display a decimal in scientific notation with Python?

Sometimes, we want to display a decimal in scientific notation with Python.

In this article, we’ll look at how to display a decimal in scientific notation with Python.

How to display a decimal in scientific notation with Python?

To display a decimal in scientific notation with Python, we can use the %.2E format string.

For instance, we write:

from decimal import Decimal

print('%.2E' % Decimal('40800000000.00000000000000'))

We create a decimal number object with the Decimal constructor and a number string.

Then we use %.2E to return a string with the number formatted into scientific notation.

Therefore, print prints '4.08E+10'.

Conclusion

To display a decimal in scientific notation with Python, we can use the %.2E format string.