简体   繁体   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位数字感到困惑。

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位数的数字只有74作为数字:

max 2 digits number in base-2 is 11b ie 3 , so: 以2为基的最大2位数字是11b3 ,所以:

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.

相关问题 打印所有 n 位数字,其数字的乘积等于给定的产品 python - Print all n-digit numbers whose product of digits equals to given product python 如何形成给定数字的所有可能组合以形成不重复数字的n位数字?(python) - How to form all possible combination of given digits to form a n-digit number without repetition of digits?(python) 在python中生成所有3位回文数的列表 - Generate list of all palindromic numbers of 3 digits in python 如何生成一个 3 位数字的列表,它们的数字总和等于 17? - how to generate a list of 3-digit numbers the sum of their digits equal 17? 编写一个 Python 程序,读取一个正整数 n 并找出 1 到 n 之间所有奇数的平均值 - Write a Python program that reads a positive integer n and finds the average of all odd numbers between 1 and n 使用itertools生成所有n位数字,范围在1位数字范围内 - Generate all n digit numbers using itertools, with bounds on range of 1st digit 递归生成所有数位和为 n 的 k 位数字 - Recursively generate all k-digit numbers whose digit-sum is n 如何生成所有数字都是偶数的数字列表? - How to generate a list of numbers where all digits are even? 如何在 python 中生成 N 次 function 并计算位数? - How to generate function n times in python and count digits? N 以下所有包含数字 7 的数字的总和 - the sum of all numbers below N that contain the digit 7
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM