繁体   English   中英

如何在python中编写程序以生成仅以4和7为数字的所有n位数字?

[英]How to write program in python to generate all n digit numbers with only 4 and 7 as their digits?

我尝试使用itertools.permutations生成排列,但是我对如何使用n位数字感到困惑。

我会改用itertools.product

In [26]: for i in itertools.product(['4', '7'], repeat=2):
   ....:     print int(''.join(i))
   ....:
44
47
74
77

repeat参数是您的n

我会用二进制的,如果你需要的所有2位数的数字只有74作为数字:

以2为基的最大2位数字是11b3 ,所以:

0 => 00b
1 => 01b
2 => 10b
3 => 11b

然后将0替换为4 ,将1替换为7 (任意),得到: 44, 47, 74, 77

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM