如何在Python中创建每个3位数字组合的列表?
我正在尝试编写一个将输出的Python脚本:
000
001
002
... 等,
但是我遇到了困难。 到目前为止,我所拥有的是:
from itertools import product
list = [x for x in range(0, 10) if True]
for x in product(list, repeat=3):
list3 = list(x)
def convert(l):
c = [str(i) for i in l]
list2 = int("".join(c))
return(list2)
print(convert(list3))
但这只是输出:
999
我不确定如何获取完整列表。 如果我评论转换函数,它会提供多个数字列表,例如:
[0,0,0]
[0,0,0,1]
...
任何帮助都将不胜感激,我敢肯定我缺少简单的东西。
I'm trying to write a python script that would output:
000
001
002
...
etc
but I'm running into difficulties.
What I have so far is:
from itertools import product
list = [x for x in range(0, 10) if True]
for x in product(list, repeat=3):
list3 = list(x)
def convert(l):
c = [str(i) for i in l]
list2 = int("".join(c))
return(list2)
print(convert(list3))
but this only outputs:
999
I'm not sure how to get the full list.
If I comment out the convert function it provides multiple lists of the numbers, like so:
[0, 0, 0]
[0, 0, 1]
...
Any help would be appreciated, I'm pretty sure I'm missing something simple.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您正在考虑它。
You're overthinking it.
如果要使用itertools,也可以做:
itertools.combinations上的文档在这里: https://docs.python.org/3/library/itertools.html#itertools.combinations
if you want to use itertools you can also do:
documentation on itertools.combinations is here: https://docs.python.org/3/library/itertools.html#itertools.combinations
如果允许重复数字,您可以尝试一下
,然后您可以尝试
You can try this
If repetition of digits are allowed, then you can try this