您好,登錄后才能下訂單哦!
小編給大家分享一下python如何實現求解列表中元素的排列和組合問題,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
使用python的itertools模塊來完成這個工作,一共解決四個問題:
1.生成排列,列表中元素不允許重復出現
2.生成排列,列表中元素可以重復出現
3.生成組合,不限元素個數,列表中元素不允許重復出現
4.生成組合,不限元素個數,列表中元素可以重復出現
因為大家都有排列組合的知識這里就不累贅了,問題很簡單,下面看具體的實現:
#!usr/bin/env python #encoding:utf-8 ''''' __Author__:沂水寒城 功能:求解列表中元素的排列和組合問題 ''' from itertools import product from itertools import combinations import itertools def test_func1(num_list): ''''' 生成排列 列表中元素不允許重復出現 排列數計算為:n!,其中n為num_list列表中元素個數 ''' tmp_list = itertools.permutations(num_list) res_list=[] for one in tmp_list: res_list.append(one) print res_list print '元素不允許重復出現排列總數為:', len(res_list) def test_func11(num_list): ''''' 生成排列 列表中元素可以重復出現 排列總數計算為:(n*n*n...*n),一共n個n相乘 ''' num=len(num_list) res_list=list(product(num_list,repeat=num)) print res_list print '元素可以重復出現排列總數為:', len(res_list) def test_func2(num_list): ''''' 生成組合,不限元素個數 列表中元素不允許重復出現 組合數計算為:2^n,其中n為num_list列表中元素個數 ''' res_list=[] for i in range(len(num_list)+1): res_list+=list(combinations(num_list, i)) print res_list print '元素不允許重復出現組合總數為:', len(res_list) def test_func22(num_list): ''''' 生成組合,不限元素個數 列表中元素可以重復出現 ''' res_list=[] num_list1=[str(i) for i in num_list] for i in range(0,len(num_list)+1): res_list+=[''.join(x) for x in itertools.product(*[num_list1] * i)] print res_list print '元素可以重復出現組合總數為:', len(res_list) if __name__ == '__main__': num_list=[1,2,3,4] test_func1(num_list) print '-------------------------------------' test_func11(num_list) print '-------------------------------------' test_func2(num_list) print '-------------------------------------' test_func22(num_list)
結果如下:
[(1, 2, 3, 4), (1, 2, 4, 3), (1, 3, 2, 4), (1, 3, 4, 2), (1, 4, 2, 3), (1, 4, 3, 2), (2, 1, 3, 4), (2, 1, 4, 3), (2, 3, 1, 4), (2, 3, 4, 1), (2, 4, 1, 3), (2, 4, 3, 1), (3, 1, 2, 4), (3, 1, 4, 2), (3, 2, 1, 4), (3, 2, 4, 1), (3, 4, 1, 2), (3, 4, 2, 1), (4, 1, 2, 3), (4, 1, 3, 2), (4, 2, 1, 3), (4, 2, 3, 1), (4, 3, 1, 2), (4, 3, 2, 1)] 元素不允許重復出現排列總數為: 24 ------------------------------------- [(1, 1, 1, 1), (1, 1, 1, 2), (1, 1, 1, 3), (1, 1, 1, 4), (1, 1, 2, 1), (1, 1, 2, 2), (1, 1, 2, 3), (1, 1, 2, 4), (1, 1, 3, 1), (1, 1, 3, 2), (1, 1, 3, 3), (1, 1, 3, 4), (1, 1, 4, 1), (1, 1, 4, 2), (1, 1, 4, 3), (1, 1, 4, 4), (1, 2, 1, 1), (1, 2, 1, 2), (1, 2, 1, 3), (1, 2, 1, 4), (1, 2, 2, 1), (1, 2, 2, 2), (1, 2, 2, 3), (1, 2, 2, 4), (1, 2, 3, 1), (1, 2, 3, 2), (1, 2, 3, 3), (1, 2, 3, 4), (1, 2, 4, 1), (1, 2, 4, 2), (1, 2, 4, 3), (1, 2, 4, 4), (1, 3, 1, 1), (1, 3, 1, 2), (1, 3, 1, 3), (1, 3, 1, 4), (1, 3, 2, 1), (1, 3, 2, 2), (1, 3, 2, 3), (1, 3, 2, 4), (1, 3, 3, 1), (1, 3, 3, 2), (1, 3, 3, 3), (1, 3, 3, 4), (1, 3, 4, 1), (1, 3, 4, 2), (1, 3, 4, 3), (1, 3, 4, 4), (1, 4, 1, 1), (1, 4, 1, 2), (1, 4, 1, 3), (1, 4, 1, 4), (1, 4, 2, 1), (1, 4, 2, 2), (1, 4, 2, 3), (1, 4, 2, 4), (1, 4, 3, 1), (1, 4, 3, 2), (1, 4, 3, 3), (1, 4, 3, 4), (1, 4, 4, 1), (1, 4, 4, 2), (1, 4, 4, 3), (1, 4, 4, 4), (2, 1, 1, 1), (2, 1, 1, 2), (2, 1, 1, 3), (2, 1, 1, 4), (2, 1, 2, 1), (2, 1, 2, 2), (2, 1, 2, 3), (2, 1, 2, 4), (2, 1, 3, 1), (2, 1, 3, 2), (2, 1, 3, 3), (2, 1, 3, 4), (2, 1, 4, 1), (2, 1, 4, 2), (2, 1, 4, 3), (2, 1, 4, 4), (2, 2, 1, 1), (2, 2, 1, 2), (2, 2, 1, 3), (2, 2, 1, 4), (2, 2, 2, 1), (2, 2, 2, 2), (2, 2, 2, 3), (2, 2, 2, 4), (2, 2, 3, 1), (2, 2, 3, 2), (2, 2, 3, 3), (2, 2, 3, 4), (2, 2, 4, 1), (2, 2, 4, 2), (2, 2, 4, 3), (2, 2, 4, 4), (2, 3, 1, 1), (2, 3, 1, 2), (2, 3, 1, 3), (2, 3, 1, 4), (2, 3, 2, 1), (2, 3, 2, 2), (2, 3, 2, 3), (2, 3, 2, 4), (2, 3, 3, 1), (2, 3, 3, 2), (2, 3, 3, 3), (2, 3, 3, 4), (2, 3, 4, 1), (2, 3, 4, 2), (2, 3, 4, 3), (2, 3, 4, 4), (2, 4, 1, 1), (2, 4, 1, 2), (2, 4, 1, 3), (2, 4, 1, 4), (2, 4, 2, 1), (2, 4, 2, 2), (2, 4, 2, 3), (2, 4, 2, 4), (2, 4, 3, 1), (2, 4, 3, 2), (2, 4, 3, 3), (2, 4, 3, 4), (2, 4, 4, 1), (2, 4, 4, 2), (2, 4, 4, 3), (2, 4, 4, 4), (3, 1, 1, 1), (3, 1, 1, 2), (3, 1, 1, 3), (3, 1, 1, 4), (3, 1, 2, 1), (3, 1, 2, 2), (3, 1, 2, 3), (3, 1, 2, 4), (3, 1, 3, 1), (3, 1, 3, 2), (3, 1, 3, 3), (3, 1, 3, 4), (3, 1, 4, 1), (3, 1, 4, 2), (3, 1, 4, 3), (3, 1, 4, 4), (3, 2, 1, 1), (3, 2, 1, 2), (3, 2, 1, 3), (3, 2, 1, 4), (3, 2, 2, 1), (3, 2, 2, 2), (3, 2, 2, 3), (3, 2, 2, 4), (3, 2, 3, 1), (3, 2, 3, 2), (3, 2, 3, 3), (3, 2, 3, 4), (3, 2, 4, 1), (3, 2, 4, 2), (3, 2, 4, 3), (3, 2, 4, 4), (3, 3, 1, 1), (3, 3, 1, 2), (3, 3, 1, 3), (3, 3, 1, 4), (3, 3, 2, 1), (3, 3, 2, 2), (3, 3, 2, 3), (3, 3, 2, 4), (3, 3, 3, 1), (3, 3, 3, 2), (3, 3, 3, 3), (3, 3, 3, 4), (3, 3, 4, 1), (3, 3, 4, 2), (3, 3, 4, 3), (3, 3, 4, 4), (3, 4, 1, 1), (3, 4, 1, 2), (3, 4, 1, 3), (3, 4, 1, 4), (3, 4, 2, 1), (3, 4, 2, 2), (3, 4, 2, 3), (3, 4, 2, 4), (3, 4, 3, 1), (3, 4, 3, 2), (3, 4, 3, 3), (3, 4, 3, 4), (3, 4, 4, 1), (3, 4, 4, 2), (3, 4, 4, 3), (3, 4, 4, 4), (4, 1, 1, 1), (4, 1, 1, 2), (4, 1, 1, 3), (4, 1, 1, 4), (4, 1, 2, 1), (4, 1, 2, 2), (4, 1, 2, 3), (4, 1, 2, 4), (4, 1, 3, 1), (4, 1, 3, 2), (4, 1, 3, 3), (4, 1, 3, 4), (4, 1, 4, 1), (4, 1, 4, 2), (4, 1, 4, 3), (4, 1, 4, 4), (4, 2, 1, 1), (4, 2, 1, 2), (4, 2, 1, 3), (4, 2, 1, 4), (4, 2, 2, 1), (4, 2, 2, 2), (4, 2, 2, 3), (4, 2, 2, 4), (4, 2, 3, 1), (4, 2, 3, 2), (4, 2, 3, 3), (4, 2, 3, 4), (4, 2, 4, 1), (4, 2, 4, 2), (4, 2, 4, 3), (4, 2, 4, 4), (4, 3, 1, 1), (4, 3, 1, 2), (4, 3, 1, 3), (4, 3, 1, 4), (4, 3, 2, 1), (4, 3, 2, 2), (4, 3, 2, 3), (4, 3, 2, 4), (4, 3, 3, 1), (4, 3, 3, 2), (4, 3, 3, 3), (4, 3, 3, 4), (4, 3, 4, 1), (4, 3, 4, 2), (4, 3, 4, 3), (4, 3, 4, 4), (4, 4, 1, 1), (4, 4, 1, 2), (4, 4, 1, 3), (4, 4, 1, 4), (4, 4, 2, 1), (4, 4, 2, 2), (4, 4, 2, 3), (4, 4, 2, 4), (4, 4, 3, 1), (4, 4, 3, 2), (4, 4, 3, 3), (4, 4, 3, 4), (4, 4, 4, 1), (4, 4, 4, 2), (4, 4, 4, 3), (4, 4, 4, 4)] 元素可以重復出現排列總數為: 256 ------------------------------------- [(), (1,), (2,), (3,), (4,), (1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4), (1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4), (1, 2, 3, 4)] 元素不允許重復出現組合總數為: 16 ------------------------------------- ['', '1', '2', '3', '4', '11', '12', '13', '14', '21', '22', '23', '24', '31', '32', '33', '34', '41', '42', '43', '44', '111', '112', '113', '114', '121', '122', '123', '124', '131', '132', '133', '134', '141', '142', '143', '144', '211', '212', '213', '214', '221', '222', '223', '224', '231', '232', '233', '234', '241', '242', '243', '244', '311', '312', '313', '314', '321', '322', '323', '324', '331', '332', '333', '334', '341', '342', '343', '344', '411', '412', '413', '414', '421', '422', '423', '424', '431', '432', '433', '434', '441', '442', '443', '444', '1111', '1112', '1113', '1114', '1121', '1122', '1123', '1124', '1131', '1132', '1133', '1134', '1141', '1142', '1143', '1144', '1211', '1212', '1213', '1214', '1221', '1222', '1223', '1224', '1231', '1232', '1233', '1234', '1241', '1242', '1243', '1244', '1311', '1312', '1313', '1314', '1321', '1322', '1323', '1324', '1331', '1332', '1333', '1334', '1341', '1342', '1343', '1344', '1411', '1412', '1413', '1414', '1421', '1422', '1423', '1424', '1431', '1432', '1433', '1434', '1441', '1442', '1443', '1444', '2111', '2112', '2113', '2114', '2121', '2122', '2123', '2124', '2131', '2132', '2133', '2134', '2141', '2142', '2143', '2144', '2211', '2212', '2213', '2214', '2221', '2222', '2223', '2224', '2231', '2232', '2233', '2234', '2241', '2242', '2243', '2244', '2311', '2312', '2313', '2314', '2321', '2322', '2323', '2324', '2331', '2332', '2333', '2334', '2341', '2342', '2343', '2344', '2411', '2412', '2413', '2414', '2421', '2422', '2423', '2424', '2431', '2432', '2433', '2434', '2441', '2442', '2443', '2444', '3111', '3112', '3113', '3114', '3121', '3122', '3123', '3124', '3131', '3132', '3133', '3134', '3141', '3142', '3143', '3144', '3211', '3212', '3213', '3214', '3221', '3222', '3223', '3224', '3231', '3232', '3233', '3234', '3241', '3242', '3243', '3244', '3311', '3312', '3313', '3314', '3321', '3322', '3323', '3324', '3331', '3332', '3333', '3334', '3341', '3342', '3343', '3344', '3411', '3412', '3413', '3414', '3421', '3422', '3423', '3424', '3431', '3432', '3433', '3434', '3441', '3442', '3443', '3444', '4111', '4112', '4113', '4114', '4121', '4122', '4123', '4124', '4131', '4132', '4133', '4134', '4141', '4142', '4143', '4144', '4211', '4212', '4213', '4214', '4221', '4222', '4223', '4224', '4231', '4232', '4233', '4234', '4241', '4242', '4243', '4244', '4311', '4312', '4313', '4314', '4321', '4322', '4323', '4324', '4331', '4332', '4333', '4334', '4341', '4342', '4343', '4344', '4411', '4412', '4413', '4414', '4421', '4422', '4423', '4424', '4431', '4432', '4433', '4434', '4441', '4442', '4443', '4444'] 元素可以重復出現組合總數為: 341 [Finished in 0.4s]
看完了這篇文章,相信你對“python如何實現求解列表中元素的排列和組合問題”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。