簡體   English   中英

給定列表中的字符串,創建所有可能的組合

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

你們將如何創建給定字符串的組合?

示例:我有一個名為john的同事,我想看看他和其他3個同事一起時的樣子。

公司人數為136人。

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

預期產量:

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']

我試過使用itertools排列。 我擁有的名稱存儲在sql db中。 我嘗試更改sql查詢以對目標合作者進行優先級排序,並針對其他合作者進行迭代。

您可以為此使用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