简体   繁体   中英

How can I generate a list of words containing only a specific number of letters in specific positions?

我想生成所有单词的列表,这些单词在特定位置(例如s,b,a,d或a在位置1,c,w,o,f,b或d在位置2等)处于特定位置。我知道哪些字母可以位于哪个位置,但是我不知道如何在特定位置生成这些字母集的所有可能组合。

I'd use itertools.product() :

In [6]: from itertools import product

In [7]: [''.join(letters) for letters in product('sbad', 'cwofbd')]
Out[7]: 
['sc',
 'sw',
 'so',
 'sf',
 'sb',
 'sd',
 'bc',
 'bw',
 'bo',
 'bf',
 'bb',
 'bd',
 'ac',
 'aw',
 'ao',
 'af',
 'ab',
 'ad',
 'dc',
 'dw',
 'do',
 'df',
 'db',
 'dd']

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