Reputation: 19
My input is looking like this right now:
[6, 7, 5, 2, 9, 9]
, [7, 1, 5, 6, 7]
, [1, 2, 0, 6, 3, 3, 8]
, [8, 0, 5, 1, 2, 3]
I want to make a list looking like this:
[675299, 71567, 1206338, 805123]
How can I do that?
Upvotes: 1
Views: 200
Reputation: 1023
This is variant without string conversions:
>>> [sum([10**k*j for k,j in enumerate(i)]) for i in l]
[675299, 71567, 1206338, 805123]
Upvotes: 1
Reputation: 13662
If you're drunk you'll find this as straightforward
>>> l = [6, 7, 5, 2, 9, 9], [7, 1, 5, 6, 7], [1, 2, 0, 6, 3, 3, 8], [8, 0, 5, 1, 2, 3]
>>> l = eval (str(l).replace(", ","").replace("][",","))
>>> l
[675299, 71567, 1206338, 805123]
Upvotes: 0
Reputation: 5157
The problem is that .join()
method only works with strings... so when you try to use it with integers, it doesn't work!
You can achieve what you want using the following code:
lst = [[6, 7, 5, 2, 9, 9],[7, 1, 5, 6, 7],[1, 2, 0, 6, 3, 3, 8],[8, 0, 5, 1, 2, 3]]
new_lst = []
# i'm going to iter over the list to join the items inside of it
# then i'll create a new number by joining each item inside each input list
# and then append it to a new list
for item in lst:
new_number = ''.join(str(x) for x in item)
new_number = int(new_number)
new_lst.append(new_number)
print new_lst
Output: [675299, 71567, 1206338, 805123]
If you want to study this topic, search: join list integers python
Upvotes: 0
Reputation: 7322
arrays = [6, 7, 5, 2, 9, 9], [7, 1, 5, 6, 7], [1, 2, 0, 6, 3, 3, 8], [8, 0, 5, 1, 2, 3]
str_arrays = [[str(number) for number in array] for array in arrays]
[int(''.join(array)) for array in str_arrays]
Upvotes: 0
Reputation: 5796
transforming a list of digits to an integer is as simple as:
l = [6, 7, 5, 2, 9, 9]
n = int(''.join(map(str, l)))
where n
holds your result.
adapting this to a list of lists of digits is trivial:
l = [
[6, 7, 5, 2, 9, 9],
[7, 1, 5, 6, 7],
[1, 2, 0, 6, 3, 3, 8],
[8, 0, 5, 1, 2, 3]
]
n = [ int(''.join(map(str, t))) for t in l ]
again, n
holds your result:
>>> print(n)
[675299, 71567, 1206338, 805123]
Upvotes: 4