题目1:有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?
import itertools list5=[1,2,3,4] newiter=itertools.permutations(list5,3) #这个能满足要求,A(4,3)=(4)!/(4-3)!=4*3*2*1/1 for i in newiter: print(i) ‘‘‘ (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) ‘‘‘
【编程小结】
1、复制list的注意事项
mlist=[1,2,3,5] tlist=mlist.copy() #说明:这里tlist与mlist分别指向两个对象(把mlist指向的对象copy一份,tlist指向copy对象) i=tlist.pop(0) print(‘tlist‘, tlist) #tlist [2, 3, 5] print(‘mlist‘, mlist) #mlist [1, 2, 3, 5] mlist=[1,2,3,5] tlist=mlist #若用tlist=mlist,则tlist指向和mlist一样 i=tlist.pop(0) print(‘tlist‘, tlist) #tlist [2, 3, 5] print(‘mlist‘, mlist) #mlist [2, 3, 5]
2、python中itertools的使用,参照:http://www.10qianwan.com/articledetail/35968.html
原文:https://www.cnblogs.com/ww-xiaowei/p/12582773.html