将整数划分为列表

2024-05-19 01:13:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我想制作一个计数器,使列表中有3个值以不同的间隔计数。例如,列表[0,0,0]的计数应如下[0,0,1]=>;[0,0,2]=>;[0,0,3]直到每个索引中出现“999”[99999999]

当列表[2]达到999时,列表[1]应增加1并从零开始计数

这就是我所拥有的:

thisList = ["%03d" % x for x in range(1000)] #produces a list of increasing numbers
trueList = []

for i in range(0, len(thisList)):
  trueList.append([int(d) for d in str(thisList[i])]) #Divides the list into each of the individual integers

print(trueList)

有什么建议吗


Tags: oftheingt列表for间隔计数器
2条回答

您也可以在不使用itertools的情况下执行此操作:

counter = ( [n//1000000,(n//1000%1000),n%1000] for n in range(1000**3) )

# counter will contain the same output as itertools.product(range(1000), repeat=3)

for c in counter: print(c)

[0, 0, 0]
[0, 0, 1]
[0, 0, 2]
[0, 0, 3]
[0, 0, 4]
...
[0, 0, 998]
[0, 0, 999]
[0, 1, 0]
[0, 1, 1]
[0, 1, 2]
...
[0, 999, 998]
[0, 999, 999]
[1, 0, 0]
[1, 0, 1]
[1, 0, 2]

使用itertools.product

trueList = list(product(range(1000), repeat=3))

相关问题 更多 >

    热门问题