I have a list of integers, which I would like to convert to a number:
NumList = [1, 2, 3] num = magic (numlist) print number, type (number) & gt; & Gt; & Gt; 123, & lt; Type 'int' & gt; What's the best way to implement the magic function? Edit
I found, but it seems that there should be a better way. One bit over-explain: DEF magic (newslist): # [1, 2] 2,3] s = map (str, numList) # ['1', '2', '3'] s = ''. Joint (s) # '123 s = int (s) # 123 return s # I Maybe I'll write it: def magic (numlist): s = '' .join (map (str, numlist)) return int (s) # one a liner number = int (''. Joining (str, numList) ) # Functional: s = less (using lambda x, y: x + str (y), numlist, '') num = int (s) # did not forget anything underlying: s = filter (str.isdigit, repr (Numlist) num = int (s)
Comments
Post a Comment