2016-08-19 41 views
1

我正在解决一个有趣的问题,我发现。我给出的代码给出了一堆子列表,比如(1,2,3,0,0)。有没有办法将该子列表转换为数字12300并将其附加到新列表perm2?我必须为相当多的子列表执行此操作,所以最好这是一个我可以在整个列表中运行的函数(即,它将遍历列表,对每个数字进行转换,并将每个新数字附加到新的列表,尽管旧列表将保持完全一样)。子列表编号

到目前为止,我的代码

import itertools 
digits = [1,2,3,0,0] 
perm = list(itertools.permutations(digits)) 
perm2 = [] 

print perm 
def lst_var (lst): 
    for i in lst: 
     litem = lst[i] 
     #conversion takes place 
     perm2.append(v) 

lst_var(perm) 

但我真的不知道该怎么办的转化,我不能在任何地方找到一个解决方案。任何帮助,将不胜感激。

谢谢!

+4

提示:12300 = 1 * 10000 + 2 * 1000 + 3 * 100 + 0 * 10 + 0 –

回答

5

这是一个从数字列表中创建一个整数的函数。此功能的优点是它不会整数列表转换成字符串列表,这是一个相当昂贵的操作:

def list_to_int(ls): 
    num = 0 
    for digit in ls: 
     num *= 10 
     num += digit 
    return num 

适用于你的例子:

list_to_int([1,2,3,0,0]) 

为了将它应用到子列表列表中,您可以使用列表理解,或者我个人比较喜欢map

sublists = [[7, 6, 6], [5, 7, 6], [9, 0, 9, 0], [8, 9, 5, 7, 8, 4]] 
map(list_to_int, sublists) 

[766,576,9090,895784]

所以,下面这个模型你的代码最终会看起来像:

digits = [1,2,3,0,0] 
perm = map(list_to_int, itertools.permutations(digits)) 
+1

谢谢你的解决方案! – heather

+1

如果你在'num + = digit'之前做'num * = 10',你可以避免用10除以 –

+0

谢谢@AndreaCorbellini。刚才在我的回答中采纳了你的建议。 – pzp

0
digits = [1,2,3,0,0] 
int(''.join([str(x) for x in digits])) 
2

你这里有几个不同的方法来解决这个问题:

perm2 = [int(''.join(str(i) for i in sublist)) for sublist in perm]

2.perm2 = [int(''.join(map(str, sublist))) for sublist in perm]

高性能数学版:

3.print [reduce(lambda x, y: 10 * x + y, sublist) for sublist in perm]

4.print map(lambda x: reduce(lambda x, y: 10 * x + y, x), perm)

此方法的列表转换成这种形式的字符串 - >例:[1, 2, 3, 4, 5]首先使用repr()然后切片返回子列表。

5.print [int(repr(sublist)[1::3]) for sublist in perm]

样本输出:

>>> import itertools 
>>> digits = [1,2,3,0,0] 
>>> perm = list(itertools.permutations(digits)) 
>>> perm2 = [int(''.join(map(str, sublist))) for sublist in perm] 
>>> print perm2 
[12300, 12300, 12030, 12003, 12030, 12003, 13200, 13200, 13020, 13002, 13020, 13002, 10230, 10203, 10320, 10302, 10023, 10032, 10230, 10203, 10320, 10302, 10023, 10032, 21300, 21300, 21030, 21003, 21030, 21003, 23100, 23100, 23010, 23001, 23010, 23001, 20130, 20103, 20310, 20301, 20013, 20031, 20130, 20103, 20310, 20301, 20013, 20031, 31200, 31200, 31020, 31002, 31020, 31002,0,0, 32010, 32001, 32010, 32001, 30120, 30102, 30210, 30201, 30012, 30021, 30120, 30102, 30210, 30201, 30012, 30021, 1230, 1203, 1320, 1302, 1023, 1032, 2130, 2103, 2310, 2301, 2013, 2031, 3120, 3102,, 3201, 3012, 3021, 123, 132, 213, 231, 312, 321, 1230, 1203, 1320, 1302, 1023, 1032, 2130, 2103, 2310, 2301, 2013, 2031, 3120, 3102,, 3201, 3012, 3021, 123, 132, 213, 231, 312, 321] 

一些基准:

from timeit import timeit 
repeat = 1000000 
print 'Solution 1 took ->', timeit("import itertools;[int(''.join(str(i) for i in sublist)) for sublist in list(itertools.permutations([1,2,3,0,0]))]", number=repeat), 'secs' 
print 'Solution 2 took ->', timeit("import itertools;[int(''.join(map(str, sublist))) for sublist in list(itertools.permutations([1,2,3,0,0]))]", number=repeat), 'secs' 
print 'Solution 3 took ->', timeit("import itertools;map(lambda x: reduce(lambda x, y: 10 * x + y, x), list(itertools.permutations([1,2,3,0,0])))", number=repeat), 'secs' 
print 'Solution 4 took ->', timeit("import itertools;[reduce(lambda x, y: 10 * x + y, sublist) for sublist in list(itertools.permutations([1,2,3,0,0]))]", number=repeat), 'secs' 
print 'Solution 5 took ->', timeit("import itertools;[int(repr(sublist)[1::3]) for sublist in list(itertools.permutations([1,2,3,0,0]))]", number=repeat), 'secs' 

结果(重复= 1000000):

Solution 1 took -> 242.802856922 secs 
Solution 2 took -> 153.20646596 secs 
Solution 3 took -> 97.4842221737 secs 
Solution 4 took -> 87.8391051292 secs 
Solution 5 took -> 122.897110224 secs 
+0

谢谢,这个作品非常漂亮,而且代码少!我会接受这个。 – heather

+0

没问题,欢呼声。 – ospahiu

+0

也增加了一个较短的理解。 – ospahiu