Let's say I have a list in python, for example:
list=[1,2,3,4,5]
How to combine the list so that it becomes:
list= [12345]
If anyone has a way to do this, we will be very grateful!
>>> list=[1,2,3,4,5] >>> k = [str(x) for x in list] >>> k ['1', '2', '3', '4', '5'] >>> "".join(k) '12345' >>> ["".join(k)] ['12345'] >>> >>> [int("".join(k))] [12345] >>>
reduce(lambda x,y:10*x+y, [1,2,3,4,5]) # returns 12345
This is probably better:
"%s" * len(L) % tuple(L)
which can handle:
>>> L=[1, 2, 3, '456', '7', 8] >>> "%s"*len(L) % tuple(L) '12345678'
list=[int("".join(map(str,list)))]
a = [1,2,3,4,5] result = [int("".join(str(x) for x in a))]
Is this really what you mean by list merging? You understand that a Python list may contain things other than numbers, right? You understand that Python is strongly typed and will not allow you to βaddβ strings to numbers or vice versa, right? What should be the result of the "merging" of the list [1, 2, "hi mom"] ?
[1, 2, "hi mom"]
[int(reduce(lambda x,y: str(x) + str(y),range(1,6)))]