Question

I'm very new to Python and have relied on PHP as my go to server side scripting language of choice for years now. I'm using Python's Itertools to generate a list of combinations of way that a 6 digit number could be arranged.

import itertools
import threading

def test(full) :
   #print full
   codes = list(itertools.product([1,2,3,4,5,6,7,8,9,0], repeat=6))
   count = len(codes)

   for num in range(0, count):
       item = codes[num]
       print item

item is returned as what appears to look like an array. It's always a series of 6 number separated by commas and inside of parentheses.

(0, 1, 2, 3, 4, 5)

How can I get the above value to be formatted as is below?

012345

Thanks

Was it helpful?

Solution

This would be a nice way of doing that

>>> from itertools import imap
>>> ''.join(imap(str, item))
012345

OTHER TIPS

What appears to look like an array is actually a tuple. But that is not crucial here, because you can use the str.join method to create a string out of the elements of any iterable (provided these elements are convertible to str):

>>> tpl = (0, 1, 2, 3, 4, 5)
>>> s = ''.join(str(i) for i in tpl)
>>> print s
012345

You can get more information on str.join by typing help(str.join) in the interactive interpreter.

>>> series = (0, 1, 2, 3, 4, 5)
>>> ''.join(str(x) for x in series)
012345

The above will give you a string with all elements from (0, 1, 2, 3, 4, 5) tuple. You can also convert it to int type (leading zero won't be preserved):

>>> int(''.join(str(x) for x in series))
12345
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top