如何在Python中创建每个3位数字组合的列表?

发布于 2025-02-09 06:56:31 字数 529 浏览 3 评论 0原文

我正在尝试编写一个将输出的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

弄潮 2025-02-16 06:56:31

您正在考虑它。

for n in range(0, 1000):
    print(f'{n:03}')

You're overthinking it.

for n in range(0, 1000):
    print(f'{n:03}')
各自安好 2025-02-16 06:56:31
import itertools
import string

for t in itertools.product(string.digits, repeat=3):
    print("".join(t))
import itertools
import string

for t in itertools.product(string.digits, repeat=3):
    print("".join(t))
回眸一笑 2025-02-16 06:56:31

如果要使用itertools,也可以做:

from itertools import combinations

lst = [x for x in range(0, 10) if True]
combos = list(combinations(lst, 3))

itertools.combinations上的文档在这里: https://docs.python.org/3/library/itertools.html#itertools.combinations

if you want to use itertools you can also do:

from itertools import combinations

lst = [x for x in range(0, 10) if True]
combos = list(combinations(lst, 3))

documentation on itertools.combinations is here: https://docs.python.org/3/library/itertools.html#itertools.combinations

北城孤痞 2025-02-16 06:56:31

如果允许重复数字,您可以尝试一下

from itertools import permutations

digits = [0, 2, 3] #List of digits from which numbers are to be formed
n = 3 #number of digits in a number

num = [int(''.join(map(str, p))) for p in permutations(digits, n) if p[0] != 0]

print(num) #number formed are without repetition of digits

,然后您可以尝试

import itertools
rlist = list(itertools.permutations([1, 2, 3]))
plist = []
for r in rlist:
    num = ''
    for _ in r:
        num += str(_)

    plist.append(int(num))

print(plist)

You can try this

from itertools import permutations

digits = [0, 2, 3] #List of digits from which numbers are to be formed
n = 3 #number of digits in a number

num = [int(''.join(map(str, p))) for p in permutations(digits, n) if p[0] != 0]

print(num) #number formed are without repetition of digits

If repetition of digits are allowed, then you can try this

import itertools
rlist = list(itertools.permutations([1, 2, 3]))
plist = []
for r in rlist:
    num = ''
    for _ in r:
        num += str(_)

    plist.append(int(num))

print(plist)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文