有以下几个数字:1、2、3、4、5,能组成多少个互不相同且无重复数字的三位数?都是多少?

方法1:

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。 Python 3.X 练习集100题 01 随笔 第1张
import itertools
from functools import reduce

lyst = [1, 2, 3, 4, 5]
result = []

for el in itertools.permutations(lyst, 3):
    temp = reduce(lambda x,y:x * 10 +y, el)
    result.append(temp)

print(f'总共有{len(result)}个互不相同且五重复数字的三位数,分别是:')
print(result)
Python 3.X 练习集100题 01 随笔 第2张

方法2:

Python 3.X 练习集100题 01 随笔 第3张
import copy
numbers = [1, 2, 3, 4, 5]
tri = []
for i in numbers:
    d_u = copy.copy(numbers)
    d_u.remove(i)
    for j in d_u:
        u = copy.copy(d_u)
        u.remove(j)
        for k in u :
            tri.append(i * 100 + j * 10 + k)

print(tri)
print(len(tri))
Python 3.X 练习集100题 01 随笔 第4张

方法3:

Python 3.X 练习集100题 01 随笔 第5张
import itertools
count = 0
for i in itertools. product([1,2,3,4,5], repeat=3):
    if i[0] != i[1] and i[0] != i[2] and i[1] != i[2]:
        print(i[0]*100 + i[1]*10 + i[2])
        count += 1
print(count)
Python 3.X 练习集100题 01 随笔 第6张

方法4:

Python 3.X 练习集100题 01 随笔 第7张
firstNumber = [1, 2, 3, 4, 5]
count = 0
for i in firstNumber:
    secondNumber = firstNumber[:]
    secondNumber.remove(i)
    for j in secondNumber:
        thirdNumber = secondNumber[:]
        thirdNumber.remove(j)
        for k in thirdNumber:
            print(str(i) + str(j) + str(k), end=",")
            count += 1
print("一共有{}个数".format(count))
Python 3.X 练习集100题 01 随笔 第8张
扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄