子列表编号

时间:2016-08-19 19:04:28

标签: python list function

我正在努力解决我发现的有趣问题。我给出的代码给出了一堆子列表,如(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)

但我真的不知道如何进行转换,我无法在任何地方找到解决方案。任何帮助将不胜感激。

谢谢!

3 个答案:

答案 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])
  

12300

为了将其应用于子列表列表,您可以使用列表推导,或者我个人更喜欢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 :(得分:2)

您可以通过以下几种方式解决此问题:

1。 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, 32100, 32100, 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, 3210, 3201, 3012, 3021, 123, 132, 213, 231, 312, 321, 1230, 1203, 1320, 1302, 1023, 1032, 2130, 2103, 2310, 2301, 2013, 2031, 3120, 3102, 3210, 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

答案 2 :(得分:0)

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