python中的排列-itertools

itertools.product(*iterables[, repeat]):重复取

取完不放回。

itertools.product(*iterables[, repeat])
输入迭代的笛卡尔积。(跟顺序有关)

大致相当于生成器表达式中的嵌套for循环。例如,product(A, B)对于A中的x和B中的y返回相同的((x,y))。

嵌套的循环就像一个里程表,在每次迭代中使用最右边的元素。此模式创建一个字典顺序,以便如果输入的迭代器已排序,则产品元组将按排序顺序发出。

要用自身计算迭代器的乘积,请使用可选的repeat关键字参数指定重复的次数。例如,product(A, repeat=4)表示与product(A, A, A, A)相同。
如,

     product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
     product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111

如,

for i in product([1, 2, 3], [4, 5], [6, 7]):
    print i
(1, 4, 6)
(1, 4, 7)
(1, 5, 6)
(1, 5, 7)
(2, 4, 6)
(2, 4, 7)
(2, 5, 6)
(2, 5, 7)
(3, 4, 6)
(3, 4, 7)
(3, 5, 6)
(3, 5, 7)

简单的来说就是放置一个可迭代的数列、元组等,然后取中其中的值,repeat决定了每组里面可容纳的元素数量。

def product(*args, repeat=1):
    pools = [tuple(pool) for pool in args] * repeat
    result = [[]]
    for pool in pools:
        result = [x+[y] for x in result for y in pool]
    for prod in result:
        yield tuple(prod)

5取2 实现(C5取2):不重复元素,顺序无关

不重复取即是取完不放回的取法,使用combinations。

>>>from itertools import combinations
>>>combins = [c for c in  combinations(range(5), 2)]
>>>len(combins)
10
>>>combins               # 而且是按序排列
[(0, 1), (0, 2), (0, 3), (0, 4), (1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]

5取2 全排列:(A5取2):不重复,顺序有关

任取两个不同的不放回,顺序会有影响。

>>>from itertools import permutations
>>>pertumations(range(5), 2)
<itertools.permutations object at 0x0233E360>

>>>perms = permutations(range(5), 2)
>>>perms
[(0, 1), (0, 2), (0, 3), (0, 4), (1, 0), (1, 2), (1, 3), (1, 4), (2, 0), (2, 1),
 (2, 3), (2, 4), (3, 0), (3, 1), (3, 2), (3, 4), (4, 0), (4, 1), (4, 2), (4, 3)]
>>>len(perms)
20

chain用法

letters = ['a', 'b', 'c', 'd', 'e', 'f']
booleans = [1, 0, 1, 0, 0, 1]
 
    print(list(itertools.chain(letters,booleans)))
#     ['a', 'b', 'c', 'd', 'e', 'f', 1, 0, 1, 0, 0, 1]
 
    print(tuple(itertools.chain(letters,letters[3:])))
#     ('a', 'b', 'c', 'd', 'e', 'f', 'd', 'e', 'f')
 
    print(set(itertools.chain(letters,letters[3:])))
#     {'a', 'd', 'b', 'e', 'c', 'f'}
        
    print(list(itertools.chain(letters,letters[3:])))
#     ['a', 'b', 'c', 'd', 'e', 'f', 'd', 'e', 'f']
 
    for item in list(itertools.chain(letters,booleans)):
        print(item)

count() 迭代器,用于产生无穷数列

count(start=0,step=1)

i = 0
    for item in itertools.count(100,2):
        i += 1
        if i > 10 : break
        
        print(item)  

filterfalse () 和compress()

filterfalse(contintion,data)
过滤出为false的选项,例如None,就默认为过滤False,就过滤出0,
可以添加匿名函数作为判断条件。

booleans = [1, 0, 1, 0, 0, 1]
numbers = [23, 20, 44, 32, 7, 12]
 
print(list(itertools.filterfalse(None,booleans)))
#     [0, 0, 0]
print(list(itertools.filterfalse(lambda x : x < 20,numbers)))
#    [23, 20, 44, 32]

根据逻辑值,返回真值与否。

print(list(itertools.compress(letters,booleans)))

# ['a', 'c', 'f']

starmap()

针对list中的每一项,调用函数功能。starmap(func,list[]) ;
就像这样,

starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000

repeat()

repeat(10, 3) --> 10 10 10

dropwhile()

dropwhile(func, seq );当函数f执行返回假时, 开始迭代序列

dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1

takewhile()

takewhile(predicate, iterable);返回序列,当predicate为true是截止。

islice()

islice(seq[, start], stop[, step]);返回序列seq的从start开始到stop结束的步长为step的元素的迭代器

for i in islice("abcdef", 0, 4, 2):#a, c
    print i
    #其中“abcdef”是seq序列,start=0是开始,stop=4是停止,step=2是步法。

combinations_with_replacement()

for i in combinations_with_replacement([1, 2, 3], 2):
    print i
(1, 1)
(1, 2)
(1, 3)
(2, 2)
(2, 3)
(3, 3)

例子:
求质数序列中1,3,5,7,9,11,13,15三个数之和为35的三个数;

def get_three_data(data_list,amount):
    for data in list(itertools.combinations(data_list, 3)):
        if sum(data) == amount:
            print(data)
#(7, 13, 15)
#(9, 11, 15)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值