从itertool.combination列表中获取元组索引


大家早上好

我试图在变量x和y中插入由itertools.combination函数生成的元组列表中的索引[0]和索引1

在这些链接中'itertools.combination '对象是不可下标的,我发现组合中的对象是不可下标的,我想知道是否有办法把单独的元组索引放到函数

a = [10,11,12,13,14,15]
b = combinations(a,2)
x= b[0]
y= b[1]
conditions = [df[f'sma_{x}'] > df[f'sma_{y}'],
df[f'sma_{x}'] < df[f'sma_{y}']]
choices = [1, -1]
df[f'sma_{x} & sma_{y}'] = np.select(conditions, choices, default=0)

itertools.combinations返回一个生成器表达式

如果你想访问combinations结果的索引,你必须用list"消耗"它:

a = [10,11,12,13,14,15]
b = list(combinations(a,2))
x= b[0]
y= b[1]
conditions = [df[f'sma_{x}'] > df[f'sma_{y}'],
df[f'sma_{x}'] < df[f'sma_{y}']]
choices = [1, -1]
df[f'sma_{x} & sma_{y}'] = np.select(conditions, choices, default=0)