user1883614
user1883614

Reputation: 945

Join integers in a list of lists in python

This is the closest I could come up with: Convert list of ints to one number?

Basically, I have an list of lists of integers:

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

How do I get this so that it's: [234, 123, 345] as integers?

Edit: I would like to vectorize this code that I can use:

result = np.zeros(len(arr))
for i in range(len(arr)):
    result[i] = int(''.join(map(str, arr[i])))

Upvotes: 1

Views: 343

Answers (3)

AChampion
AChampion

Reputation: 30268

A slightly more mathematical way:

>>> [sum(n*10**i for i, n in enumerate(reversed(x))) for x in arr]
[234, 123, 345]

Upvotes: 0

ANVGJSENGRDG
ANVGJSENGRDG

Reputation: 153

arr = [[2,3,4], [1,2,3], [3,4,5]]
arr2 = []
for x in arr:
    z = ""
    for y in x:
        z = z + str(y)
    arr2.append(int(z))
#the results are now in arr2
print arr2

Upvotes: 0

akuiper
akuiper

Reputation: 215057

[int(''.join(map(str, x))) for x in arr]
# [234, 123, 345]

Upvotes: 4

Related Questions