Python3 100例 | 菜鸟教程 习题链接
题1:
题目:有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?
方法一:
import itertools
l = list(itertools.permutations([1,2,3,4],3))
print(len(l))
print(l)
# 24
# [(1, 2, 3), (1, 2, 4), (1, 3, 2), (1, 3, 4), (1, 4, 2), (1, 4, 3), (2, 1, 3), (2, 1, 4), (2, 3, 1), (2, 3, 4), (2, 4, 1), (2, 4, 3), (3, 1, 2), (3, 1, 4), (3, 2, 1), (3, 2, 4), (3, 4, 1), (3, 4, 2), (4, 1, 2), (4, 1, 3), (4, 2, 1), (4, 2, 3), (4, 3, 1), (4, 3, 2)]
方法二:
l = []
for i in range(1,5):
for j in range(1,5):
for k in range(1, 5):
if i != j and j != k and i != k:
l2 = []
l2.append(i)
l2.append(j)
l2.append(k)
l.append(l2)
print(len(l))
print(l)
# 24
# [[1, 2, 3