Python 子列表到数字

Python 子列表到数字,python,list,function,Python,List,Function,我正在努力解决我发现的一个有趣的问题。我的代码给出了一系列子列表,比如(1,2,3,0,0)。是否有办法将该子列表转换为数字12300并将其附加到新列表中,perm2?我必须对很多子列表执行此操作,因此最好是在整个列表上运行此函数(即,它将遍历列表,对每个数字进行转换,并将每个新数字附加到新列表中,尽管旧列表将保持完全相同) 到目前为止,我有代码 import itertools digits = [1,2,3,0,0] perm = list(itertools.permutations(di

我正在努力解决我发现的一个有趣的问题。我的代码给出了一系列子列表,比如(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)
但我真的不知道如何进行转换,而且我在任何地方都找不到解决方案。任何帮助都将不胜感激


谢谢

这里有几种不同的解决方法:

digits = [1,2,3,0,0]
int(''.join([str(x) for x in digits]))
1.
perm2=[int('''.join(str(i)表示子列表中的i))表示perm中的子列表]

2.
perm2=[int(''.join(map(str,sublist)))用于perm中的子列表]

更具性能的数学版本:

3.
为perm中的子列表打印[减少(λx,y:10*x+y,子列表)]

4.
打印地图(λx:reduce(λx,y:10*x+y,x),perm)

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

5.
在perm中为子列表打印[int(repr(子列表)[1::3])

样本输出:

>>> 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'
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
一些基准:

>>> 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'
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
结果(重复次数=1000000):

>>> 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'
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

您可以通过以下几种不同的方法来解决此问题:

1.
perm2=[int('''.join(str(i)表示子列表中的i))表示perm中的子列表]

2.
perm2=[int(''.join(map(str,sublist)))用于perm中的子列表]

更具性能的数学版本:

3.
为perm中的子列表打印[减少(λx,y:10*x+y,子列表)]

4.
打印地图(λx:reduce(λx,y:10*x+y,x),perm)

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

5.
在perm中为子列表打印[int(repr(子列表)[1::3])

样本输出:

>>> 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'
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
一些基准:

>>> 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'
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
结果(重复次数=1000000):

>>> 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'
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

下面是一个从数字列表中创建整数的函数。此函数的优点是,它不会将整数列表转换为字符串列表,这是一个成本相当高的操作:

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)
[7665769090895784]

因此,遵循该模型,您的代码最终将看起来像:

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

下面是一个从数字列表中创建整数的函数。此函数的优点是,它不会将整数列表转换为字符串列表,这是一个成本相当高的操作:

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)
[7665769090895784]

因此,遵循该模型,您的代码最终将看起来像:

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


提示:12300=1*10000+2*1000+3*100+0*10+0提示:12300=1*10000+2*1000+3*100+0*10+0谢谢,这工作起来很漂亮,代码也少了!我会接受的。还增加了一个简短的理解。@heather注意到,如果考虑性能的话,这比数学方法慢几个数量级。(基于Python 3中的TimeIt的快速运行)@ GalbDud,谢谢,我会考虑的。在这一点上,性能不是一个考虑因素,但它最终可能会成为一个考虑因素。我将使用较少的pythonic、performant数学方法补充我的答案。谢谢你,这非常有效,而且它使用的代码更少!我会接受这个。另外,增加了一个较短的理解。@希瑟注意,如果性能是考虑的话,这比数学方法慢了几量级。(基于Python 3中的TimeIt的快速运行)@ GalbDud,谢谢,我会考虑的。在这一点上,性能不是一个考虑因素,但它最终可能会成为一个考虑因素。我将用一点不那么通俗的数学方法补充我的答案。谢谢你的解决方案!如果在
num+=digit
之前执行
num*=10
,您可以避免被10除谢谢,@AndreaCorbellini。在我的回答中采纳了你的建议。谢谢你的解决方案!如果在
num+=digit
之前执行
num*=10
,您可以避免被10除谢谢,@AndreaCorbellini。我刚才在回答中采纳了你的建议。