1,2,3,4,5能组成多少个互不相同且无重复的元素?
有60种。5个数里随意抽取3个为C53 (5为下标,3为上标),这3个数可以随意组合,为A33(一个3为上标,一个3为下标),所以公式为 C53 *A33=60
用代码进行实现
# 方式1
li = [1, 2, 3, 4, 5]
new_li = []
for i in li:
for j in li:
for m in li:
if i != j and i != m and j != m:
num_str = str(i) + str(j) + str(m)
num = int(num_str)
new_li.append(num)
new_li.sort()
print(new_li)
ret = [int(str(i) + str(j) + str(k)) for k in li for j in li for i in li if i != j and i != k and j != k]
ret.sort()
print(len(ret))
print(ret)
# 方式2
import itertools
print(len(list(itertools.permutations("12345", 3))))
python内部有排列组合函数(不放回抽样排列)
product 笛卡尔积 (有放回抽样排列)
permutations 排列 (不放回抽样排列)
combinations 组合,没有重复 (不放回抽样排列)
combinations_with_replacement (有放回抽样排列)
浙公网安备 33010602011771号