python遍历list成员_python-获取N个成员的K个组和M个成员的L个组...

编辑:编辑代码以满足更新的要求(规则3).

码:

import itertools as it

def unique_group(iterable, k, n):

"""Return an iterator, comprising groups of size `k` with combinations of size `n`."""

# Build separate combinations of `n` characters

groups = ("".join(i) for i in it.combinations(iterable, n)) # 'AB', 'AC', 'AD', ...

# Build unique groups of `k` by keeping the longest sets of characters

return (i for i in it.combinations(groups, k)

if len(set("".join(i))) == sum((map(len, i)))) # ('AB', 'CD'), ('AB', 'CE'), ...

def combined(groups1, groups2):

"""Return an iterator with unique combinations of groups (k and l)."""

# Build a unique cartesian product of groups `k` and `l`, filtering non-disjoints

return (i[0] + i[1]

for i in it.product(groups1, groups2)

if set("".join(i[0])).isdisjoint(set("".join(i[-1]))))

iterable = "ABCDEFG"

g1 = unique_group(iterable, 2, 2)

g2 = unique_group(iterable, 1, 3)

result = list(combined(g1, g2))

print(len(result))

result

输出:

105

[('AB', 'CD', 'EFG'),

('AB', 'CE', 'DFG'),

...,

('BC', 'AD', 'EFG'),

('BC', 'AE', 'DFG'),

...,

]

详细信息和见解可以在demonstration中找到.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值