简体   繁体   中英

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:

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 .

I would use binary, if you need all 2-digits numbers with only 7 , 4 as digits:

max 2 digits number in base-2 is 11b ie 3 , so:

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

then replace 0 by 4 and 1 by 7 (arbitrary), giving: 44, 47, 74, 77

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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