将整数划分为列表

Dividing an integer into lists

我想制作一个计数器,使列表中有 3 个值以不同的时间间隔递增。例如,列表 [0, 0, 0] 应该像这样计数 [0, 0, 1] => [0, 0, 2] => [0,0,3] 直到每个索引中都有“999” [999,999,999].

当列表[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)

有什么建议吗?

使用itertools.product:

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

你也可以在没有 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]