简体   繁体   中英

How to join the elements of the tuple together and convert into a number

from itertools import permutations 
a,b= input().split()
a = int(a)
b = int(b)
default = -1

c = [int(i) for i in str(a)]
perm = permutations(c)
for tupl in list(perm):
    res = list(map("".join, tupl))
    #num =  ''.join(j) for j in tupl

    print("\n")
#    print( num)
    # for j in range(0,len(tupl)):
    #     print(tupl[j])
            # res = [''.join(tups) for tups in  ]

I need to join elements of tup1 to make it a number. Each permutation number must be converted to digit number

>>> a = 213
>>> for tupl in permutations(str(a)):
...     num = int(''.join(tupl))
...     print (num)
... 
213
231
123
132
321
312

Or as a one-liner

>>> res = list(map(lambda t: int(''.join(t)), permutations(str(a))))
>>> print(res)
[213, 231, 123, 132, 321, 312]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM