gpt4 book ai didi

python - Python中的排列组合

转载 作者:太空宇宙 更新时间:2023-11-04 10:20:30 25 4
gpt4 key购买 nike

python 是否有一些内置函数,通过它我可以生成这样的序列

对于i = 5,0和1有多少种不同的方式可以占据5个位置

00000
00001
00010
00011
00100
.
.
.
.
11111

对于i = 6,0和1有多少种方式可以占据6个位置:

000000
000001
000010
000011
000100
000101
.
.
.
111111

我已经熟悉itertools 排列和组合,但我无法生成这样的序列。任何帮助将不胜感激。

最佳答案

您可以使用 itertools.product

>>>from itertools import product
>>>list(product([0,1],repeat=5))

关于python - Python中的排列组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32535031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com