简体   繁体   English

给定列表中的字符串,创建所有可能的组合

[英]Creating all possible combinations given a string against a list

How would you guys go about creating a combinations given a string? 你们将如何创建给定字符串的组合?

Example: I have a co worker named john and I would like to see how he would look like when grouped with 3 other co workers. 示例:我有一个名为john的同事,我想看看他和其他3个同事一起时的样子。

The amount of people in the company is 136 people. 公司人数为136人。

Teammates = ['john', 'lizzy', 'tom', 'sarah', 'tiffany', 'max', 'james', 'alice', 'bob']

Expected output: 预期产量:

Which co worker?: John

['john', 'lizzy', 'tom', 'sarah',]
['john', 'lizzy', 'tom', 'max']
['john', 'lizzy', 'tom', 'james']
['john', 'lizzy', 'tom', 'alice']
['john', 'lizzy', 'tom', 'bob']
['john', 'tiffany', 'max', 'james']

I've tried using itertools permutations. 我试过使用itertools排列。 The names I have are stored in a sql db. 我拥有的名称存储在sql db中。 I tried changing my sql query to prioritize my target co worker and iterate against the other co workers. 我尝试更改sql查询以对目标合作者进行优先级排序,并针对其他合作者进行迭代。

You could use itertools.combinations for this: 您可以为此使用itertools.combinations:

import itertools as it

def teammate_combinations(who, group_size)
    return (x for x in it.combinations(teammates, group_size) if who in x)

teammate_combinations('john', 4)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM