tags:

views:

4209

answers:

9

I have a list of integers that I would like to convert to one number like:

numList = [1,2,3]
num = magic(numList)

print num, type(num)
>>> 123, <type 'int'>

What is the best way to implement the magic function?

Thanks for your help.

EDIT
I did find this, but it seems like there has to be a better way.

EDIT 2
Let's give some credit to Triptych and cdleary for their great answers! Thanks guys.

+27  A: 
# Over-explaining a bit:
def magic(numList):         # [1,2,3]
    s = map(str, numList)   # ['1','2','3']
    s = ''.join(s)          # '123'
    s = int(s)              # 123
    return s


# How I'd probably write it:
def magic(numList):
    s = ''.join(map(str, numList))
    return int(s)


# As a one-liner  
num = int(''.join(map(str,numList)))


# Functionally:
s = reduce(lambda x,y: x+str(y), numList, '')
num = int(s)


# Using some oft-forgotten built-ins:
s = filter(str.isdigit, repr(numList))
num = int(s)
Triptych
I had thought the map function was deprecated in favor of list comprehensions, but now I can no longer find a note to that effect. thank you, I'll be adding that back into my vocabulary.
TokenMacGuy
I assume there's a bug in your "# How I'd probably write it:" and it should be `''.join(map(str, numList))` ? Also, for your "Cleverly" option, you need to int() the result.
John Fouhy
Yes your missing map in #2. for a moment there I thought you really were doing magic!
Robert Gould
haha thanks - I was editing a bunch and missed it
Triptych
TokenMacGuy: you mean this? - http://www.artima.com/weblogs/viewpost.jsp?thread=98196 map, reduce, filter, lambda were all to go in 3k originally
Alabaster Codify
+1 for the different versions
mdec
+1 for the first two methods, the "functional" way seems a bit silly, and the filter/str.isdigit way seems like a horrible hack
dbr
+2  A: 

pseudo-code:

int magic(list nums)
{
  int tot = 0

  while (!nums.isEmpty())
  {
    int digit = nums.takeFirst()
    tot *= 10
    tot += digit
  }

  return tot
}
Andrew Medico
I think you missed the part where he was looking for a python solution :P
Dana
That's okay -- Andrew's solution was actually one of the fastest when converted to Python. +1 from me!
cdleary
It is the fastest solution (@cdleary's implementation in Python) if the list size is less than 30. http://stackoverflow.com/questions/489999/python-convert-list-of-ints-to-one-number/493944#493944
J.F. Sebastian
+3  A: 
def magic(numbers):
    return int(''.join([ "%d"%x for x in numbers]))
TokenMacGuy
nice to see someone else whose brain works like mine.
elliot42
+16  A: 
cdleary
holy shit, thats awesome....thanks for doing that!
Casey
No problem, but remember you should probably use what's most readable unless you find it's a bottleneck. I just like timing things. ;-)
cdleary
I've measured performance of the above function. The results are slightly different e.g. with_accumulator() is faster for small `digit_count`. See http://stackoverflow.com/questions/489999/python-convert-list-of-ints-to-one-number/493944#493944
J.F. Sebastian
+2  A: 
def magic(number):
    return int(''.join(str(i) for i in number))
Rex Logan
Nitpick - you can remove the `[ ]` and do the str'ing as a generation expression. `int(''.join(str(i) for i in number))` - it's.. two bytes quicker!
dbr
+2  A: 

This seems pretty clean, to me.

def magic( aList, base=10 ):
    n= 0
    for d in aList:
        n = base*n + d
    return n
S.Lott
+1  A: 

This method works in 2.x as long as each element in the list is only a single digit. But you shouldn't actually use this. It's horrible.

>>> magic = lambda l:int(`l`[1::3])
>>> magic([3,1,3,3,7])
31337
recursive
+2  A: 

Using a generator expression:

def magic(numbers):
    digits = ''.join(str(n) for n in numbers)
    return int(digits)
Ryan
+1 for no strange usage of lambda/map/etc
dbr
+1  A: 
J.F. Sebastian
That's a strange way to write it in a Python 2.6/3.0 way.. `print(''.join(str(x) for x in [1,2,3,4,5]))` will work in Python 2.5, 2.6, 3.x, probably more...
dbr
@dbr: the purpose was to use the print function. It is not recommended way, that's why I wrote "for completeness".
J.F. Sebastian