如何找到整数的所有组合?
How to find all the Combinations of an integer number?
假设我们已经给出了数字 n = 212。
我们需要把所有可能的1位、2位、3位的组合都变成一个数的长度。
In this example, the length is 3.
So combinations will be [2, 1, 2, 21, 22, 12, 212].
Tell me how to do it?
Is there any method or function in python that can help me ease the problem?
Another Example:
n = 2345
combinations = [2, 3, 4, 5, 23, 24, 25, 34, 35, 45, 234, 235, 345, 2345]
也许这可以帮到你
from itertools import combinations
number = list(input("Kindly enter the number: "))
counter = 1
answer = []
while counter < len(number)+1:
comb = combinations(number, counter)
for i in list(comb):
num = "".join(i)
answer.append(int(num))
counter += 1
print(list(set(answer)))
假设我们已经给出了数字 n = 212。 我们需要把所有可能的1位、2位、3位的组合都变成一个数的长度。
In this example, the length is 3.
So combinations will be [2, 1, 2, 21, 22, 12, 212].
Tell me how to do it?
Is there any method or function in python that can help me ease the problem?
Another Example:
n = 2345
combinations = [2, 3, 4, 5, 23, 24, 25, 34, 35, 45, 234, 235, 345, 2345]
也许这可以帮到你
from itertools import combinations
number = list(input("Kindly enter the number: "))
counter = 1
answer = []
while counter < len(number)+1:
comb = combinations(number, counter)
for i in list(comb):
num = "".join(i)
answer.append(int(num))
counter += 1
print(list(set(answer)))