1.排列組合問題:
需要用到Python的itertools模塊
import itertools a=[1,2,3] #排列,無放回的取,排列(數學公式:A32的意思) for i in itertools.permutations(a,2):#2是拿兩次,a可以是字串或者是串列 print(i) ''' (1, 2) (1, 3) (2, 1) (2, 3) (3, 1) (3, 2) ''' #組合,無放回的取,組合(數學公式:C32的意思) # for i in itertools.combinations(a,2): # print(i) ''' (1, 2) (1, 3) (2, 3) ''' #有放回的排列(笛卡爾積) # for i in itertools.product(a,repeat=2): # print(i) ''' (1, 1) (1, 2) (1, 3) (2, 1) (2, 2) (2, 3) (3, 1) (3, 2) (3, 3) ''' #有放回的組合 # for i in itertools.combinations_with_replacement(a, 2): # print(i) ''' (1, 1) (1, 2) (1, 3) (2, 2) (2, 3) (3, 3) '''
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/236377.html
標籤:Python
上一篇:租房經歷總結-----我是如何2天找到合適租房的(房東直租)簡單粗暴
下一篇:精盡Spring MVC原始碼分析 - HandlerAdapter 組件(一)之 HandlerAdapter
