Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert list of lists to list of integers

Tags:

python

list

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!

like image 837
Ali Avatar asked Aug 30 '13 03:08

Ali


People also ask

How do I turn a list into an integer?

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.

How do you convert a list of floats to a list of integers?

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.

How do you convert a list of integers to a list in Python?

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) .


1 Answers

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.

like image 143
Matt Bryant Avatar answered Sep 29 '22 21:09

Matt Bryant