【发布时间】:2023-03-22 01:42:01
【问题描述】:
给定指数的可能组合数
例如单词BANANA的索引[0]
应该给我:
{'B',
'BA',
'BAN',
'BANA',
'BANAN',
'BANANA'}
word='BANANA'
indices=[0,2,4]
def find_combinations(word:str,indices:list):
a=[''.join(l) for i in range(len(word)) for l in combinations(word, i+1)]
b=[x for x in a if x.startswith('B')]
return b
输出:
set(b)
{'B',
'BA',
'BAA',
'BAAA',
'BAAN',
'BAANA',
'BAN',
'BANA',
'BANAA',
'BANAN',
'BANANA',
'BANN',
'BANNA',
'BN',
'BNA',
'BNAA',
'BNAN',
'BNANA',
'BNN',
'BNNA'}
想要的输出:
{'B',
'BA',
'BAN',
'BANA',
'BANAN',
'BANANA'}
【问题讨论】:
-
您不需要组合并减少结果。这只是一些 prefix_sum 变体。问题是索引的用途 - 目前尚不清楚。
-
是的,你是对的@DanielHao,谢谢指出
标签: python-3.x combinations itertools