Home > Software engineering >  Get Tuple Index from itertool.combination list
Get Tuple Index from itertool.combination list

Time:10-20

Good morning to all

Im trying to insert in variable x and y the index[0] and index 1 from a list of tuples generate by the itertools.combination function

In these link 'itertools.combinations' object is not subscriptableI found that objects from combination are not subscriptable and will like to know if there´s a way to get the separate tuples index into the function

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)

CodePudding user response:

itertools.combinations return a generator expression.

If you want to access an index of the result of combinations you have to 'consume' it with 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)
  • Related