简体   繁体   中英

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.

The amount of people in the company is 136 people.

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. The names I have are stored in a sql db. I tried changing my sql query to prioritize my target co worker and iterate against the other co workers.

You could use itertools.combinations for this:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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