I need to convert a list of lists to a list of integers.
from:
L1 = [[1, 2, 3, 4], [3, 7, 1, 7], [0, 5, 6, 7], [9, 4, 5, 6]]
to:
L2 = [1234, 3717, 0567, 9456]
How can I make python recognize an integer starting with 0? Like the case L2[2]
The other question is, how can I check if items in a list are ordered?
A = [1, 2, 6, 9] ---->True
Other than this:
A == sorted(A)
You guys are FAST. Thanks!
Use int() function to Convert list to int in Python. This method with a list comprehension returns one integer value that combines all elements of the list.
The most Pythonic way to convert a list of floats fs to a list of integers is to use the one-liner fs = [int(x) for x in fs] . It iterates over all elements in the list fs using list comprehension and converts each list element x to an integer value using the int(x) constructor.
Use the join() method of Python. First convert the list of integer into a list of strings( as join() works with strings only). Then, simply join them using join() method. It takes a time complexity of O(n) .
The first question can be done by
L = [int("".join([str(y) for y in x])) for x in L]
Unfortunately, integers do not start with a 0. There is no way to do this.
Checking if A == sorted(A)
is a perfectly fine way to do this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With