Reputation: 896
I have three lists: signal, returns, and pct_list:
signal= ['signal0', 'signal1', 'signal2']
returns = ['retSCIENCE_MSFT','retLOVE_MSFT','retMUSIC_MSFT',
'retSCIENCE_YHOO', 'retLOVE_YHOO', 'retMUSIC_YHOO']
pct_list = ['Pct_MSFT', 'Pct_YHOO']
I tried using the zip function and it gave me the following result:
ret_sig_list = list(zip(returns, signal, pct_list))
ret_sig_list
Output:
[('retSCIENCE_MSFT', 'signal0', 'Pct_MSFT'),
('retLOVE_MSFT', 'signal1', 'Pct_YHOO')]
What I'm trying to get is both MSFT and YHOO lined up with the corresponding signal and percent change like so:
ret_sig_list = [('retSCIENCE_MSFT', 'signal0', 'Pct_MSFT'),
('retLOVE_MSFT', 'signal1', 'Pct_MSFT'),
('retMUSIC_MSFT', 'signal2', 'Pct_MSFT'),
('retSCIENCE_YHOO', 'signal0', 'Pct_YHOO'),
('retLOVE_YHOO', 'signal1', 'Pct_YHOO'),
('retMUSIC_YHOO','signal2', 'Pct_YHOO')]
I tried with list(zip(returns, signal, pct_list)), but it seems to default down to the lowest number of elements in the three lists, which is pct_list.
If anyone knows how I can tackle this problem, I'd be very greatful! I'd like to use the ret_sig_list with a for-loop to perform a calculation on multiple stocks percent change goes up or down to give returns based on the associated words; science, love, music, etc. I got the words from a Python package for Google Trends called Pytrends which I'd recommend, it's very cool!
Upvotes: 1
Views: 1435
Reputation: 896
So I managed to almost find a solution, it's not ideal, but close. I say not ideal cause not including the specific words from the list of ["SCIENCE", "LOVE", "MUSIC"] but it's the only way to create an ordered zip list.
stock_list = ['MSFT', 'YHOO']
stock_list_v2 = []
for stock in stock_list:
for i in range(10):
s = 'ret' + str(i) + '_' + stock
stock_list_v2.append(s)
stock_list_v2
Output:
['ret0_MSFT',
'ret1_MSFT',
'ret2_MSFT',
'ret3_MSFT',
'ret4_MSFT',
'ret5_MSFT',
'ret6_MSFT',
'ret7_MSFT',
'ret8_MSFT',
'ret9_MSFT',
'ret0_YHOO',
'ret1_YHOO',
'ret2_YHOO',
'ret3_YHOO',
'ret4_YHOO',
'ret5_YHOO',
'ret6_YHOO',
'ret7_YHOO',
'ret8_YHOO',
'ret9_YHOO']
Next signals:
sig = list(range(10))
sig
signal = ["signal{}".format(*sig) for sig in enumerate(sig)]
signal
Output:
['signal0',
'signal1',
'signal2',
'signal3',
'signal4',
'signal5',
'signal6',
'signal7',
'signal8',
'signal9']
Next, pct.
stock_list = ['MSFT', 'YHOO']
stock_list_v3 = []
for stock in stock_list:
for i in range(10):
p = 'pct' + str(i) + '_' + stock
stock_list_v3.append(p)
stock_list_v3
Output:
['pct0_MSFT',
'pct1_MSFT',
'pct2_MSFT',
'pct3_MSFT',
'pct4_MSFT',
'pct5_MSFT',
'pct6_MSFT',
'pct7_MSFT',
'pct8_MSFT',
'pct9_MSFT',
'pct0_YHOO',
'pct1_YHOO',
'pct2_YHOO',
'pct3_YHOO',
'pct4_YHOO',
'pct5_YHOO',
'pct6_YHOO',
'pct7_YHOO',
'pct8_YHOO',
'pct9_YHOO']
Now we import cycle from itertools like so.
from itertools import cycle
full_list = list(zip(stock_list_v2, cycle(signal), stock_list_v3))
full_list
Output:
[('ret0_MSFT', 'signal0', 'pct0_MSFT'),
('ret1_MSFT', 'signal1', 'pct1_MSFT'),
('ret2_MSFT', 'signal2', 'pct2_MSFT'),
('ret3_MSFT', 'signal3', 'pct3_MSFT'),
('ret4_MSFT', 'signal4', 'pct4_MSFT'),
('ret5_MSFT', 'signal5', 'pct5_MSFT'),
('ret6_MSFT', 'signal6', 'pct6_MSFT'),
('ret7_MSFT', 'signal7', 'pct7_MSFT'),
('ret8_MSFT', 'signal8', 'pct8_MSFT'),
('ret9_MSFT', 'signal9', 'pct9_MSFT'),
('ret0_YHOO', 'signal0', 'pct0_YHOO'),
('ret1_YHOO', 'signal1', 'pct1_YHOO'),
('ret2_YHOO', 'signal2', 'pct2_YHOO'),
('ret3_YHOO', 'signal3', 'pct3_YHOO'),
('ret4_YHOO', 'signal4', 'pct4_YHOO'),
('ret5_YHOO', 'signal5', 'pct5_YHOO'),
('ret6_YHOO', 'signal6', 'pct6_YHOO'),
('ret7_YHOO', 'signal7', 'pct7_YHOO'),
('ret8_YHOO', 'signal8', 'pct8_YHOO'),
('ret9_YHOO', 'signal9', 'pct9_YHOO')]
Hope that helps anyone who was curious about how to solve it :) Thanks again to PrestonM for suggesting an algo, and Juapana for helping suggest using itertools cycle.
Upvotes: 1
Reputation: 95948
Use itertools.cycle
:
In [2]: from itertools import cycle
In [3]: list(zip(returns, cycle(signal), cycle(pct_list)))
Out[3]:
[('retSCIENCE_MSFT', 'signal0', 'Pct_MSFT'),
('retLOVE_MSFT', 'signal1', 'Pct_YHOO'),
('retMUSIC_MSFT', 'signal2', 'Pct_MSFT'),
('retSCIENCE_YHOO', 'signal0', 'Pct_YHOO'),
('retLOVE_YHOO', 'signal1', 'Pct_MSFT'),
('retMUSIC_YHOO', 'signal2', 'Pct_YHOO')]
Upvotes: 3