欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python列表list排列組合操作示例

 更新時(shí)間:2018年12月18日 08:35:26   作者:DreamLee0625  
這篇文章主要介紹了Python列表list排列組合操作,涉及Python排列組合數(shù)值運(yùn)算相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了Python列表list排列組合操作。分享給大家供大家參考,具體如下:

排列

例如:

輸入為

['1','2','3']和3

輸出為

['111','112','113','121','122','123','131','132','133','211','212','213','221','222','223','231','232','233','311','312','313','321','322','323','331','332','333']

實(shí)現(xiàn)代碼:

# -*- coding:utf-8 -*-
#! pyhton2
from itertools import product
l = [1, 2, 3]
print list(product(l, l))
print list(product(l, repeat=3))

上述代碼運(yùn)行輸出:

[(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3)]
[(1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 2, 1), (1, 2, 2), (1, 2, 3), (1, 3, 1), (1, 3, 2), (1, 3, 3), (2, 1, 1), (2, 1, 2), (2, 1, 3), (2, 2, 1), (2, 2, 2), (2, 2, 3), (2, 3, 1), (2, 3, 2), (2, 3, 3), (3, 1, 1), (3, 1, 2), (3, 1, 3), (3, 2, 1), (3, 2, 2), (3, 2, 3), (3, 3, 1), (3, 3, 2), (3, 3, 3)]

組合

例如:

輸入為

[1, 2, 3]和2

輸出為

[1, 2], [1, 3], [2, 3] 不考慮順序

實(shí)現(xiàn)代碼:

# -*- coding:utf-8 -*-
#! pyhton2
from itertools import combinations
l = [1, 2, 3, 4, 5]
print list(combinations(l, 3))

上述代碼運(yùn)行輸出:

[(1, 2, 3), (1, 2, 4), (1, 2, 5), (1, 3, 4), (1, 3, 5), (1, 4, 5), (2, 3, 4), (2, 3, 5), (2, 4, 5), (3, 4, 5)]

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)學(xué)運(yùn)算技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門(mén)與進(jìn)階經(jīng)典教程

希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論