Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
I am having one array like below.
a = [1,2,3,4]
I want to make this array to single value like this:
a = [1234]
How can I do this in Python?
You can use join
to join all element in array, but you have to convert them to a string first:
>>> a = [1,2,3,4]
>>> [int(''.join([str(i) for i in a]))]
[1234]
you can do it without converting each element to string.
>>> def sum_elements(arr):
... s = 0
... for e in arr:
... s *= 10
... s += e
... return s
...
>>> a = [1, 2, 3, 4]
>>> a = [sum_elements(a)]
>>> a
[1234]
>>>
You can loop for all elements of a and add the digit multiplied by a power of 10 to reach the good value.
a = [1,2,3,4]
b = [0]
for i in xrange(len(a)):
b[0]+= (10**(len(a)-i-1))*a[i]
print a, b
[1, 2, 3, 4] [1234]