有没有办法将仅包含一个元素的列表与列表的所有其他元素组合起来?

Is there a way to get the combinations of a list of just one element to all the other elements of a list?

我正在尝试生成列表的第一个元素与所有其他元素的组合。通过首先获得所有可能的组合然后丢弃我不需要的那些,用一个小列表来做这不是一个问题。示例:

import numpy as np
import itertools

lst = ['a','b','c','d','e','f']
element_of_interest = 'a'

all_combinations = np.array(list(itertools.combinations(lst, 3)))

idx = np.where(all_combinations == element_of_interest)
combinations_of_interest = all_combinations[idx[0]]

print(combinations_of_interest)

>>> [['a' 'b' 'c']
 ['a' 'b' 'd']
 ['a' 'b' 'e']
 ['a' 'b' 'f']
 ['a' 'c' 'd']
 ['a' 'c' 'e']
 ['a' 'c' 'f']
 ['a' 'd' 'e']
 ['a' 'd' 'f']
 ['a' 'e' 'f']]

但是在我的应用程序(290 个元素的列表,r = 4)中,这种方法不够好,因为它非常低效。

有谁能想到更好的方法吗?

从列表中取出该元素,然后将其添加到所有其余组合中:

In [9]: import itertools
   ...:
   ...: lst = ['a','b','c','d','e','f']
   ...: element_of_interest = 'a'

In [11]: out = [['a'] + list(i) for i in itertools.combinations(lst[1:], 2)]

In [12]: out
Out[12]:
[['a', 'b', 'c'],
 ['a', 'b', 'd'],
 ['a', 'b', 'e'],
 ['a', 'b', 'f'],
 ['a', 'c', 'd'],
 ['a', 'c', 'e'],
 ['a', 'c', 'f'],
 ['a', 'd', 'e'],
 ['a', 'd', 'f'],
 ['a', 'e', 'f']]