[英]How to write program in python to generate all n digit numbers with only 4 and 7 as their digits?
我尝试使用itertools.permutations生成排列,但是我对如何使用n位数字感到困惑。
I would use itertools.product
instead: 我会改用
itertools.product
:
In [26]: for i in itertools.product(['4', '7'], repeat=2):
....: print int(''.join(i))
....:
44
47
74
77
The repeat
argument is your n
. repeat
参数是您的n
。
I would use binary, if you need all 2-digits numbers with only 7
, 4
as digits: 我会用二进制的,如果你需要的所有2位数的数字只有
7
, 4
作为数字:
max 2 digits number in base-2 is 11b
ie 3
, so: 以2为基的最大2位数字是
11b
即3
,所以:
0 => 00b
1 => 01b
2 => 10b
3 => 11b
then replace 0
by 4
and 1
by 7
(arbitrary), giving: 44, 47, 74, 77
然后将
0
替换为4
,将1
替换为7
(任意),得到: 44, 47, 74, 77
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.