[英]How to combine three lists of different size in order Python
I have three lists: signal, returns, and pct_list: 我有三个列表:signal,returns和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: 我尝试使用zip函数,它给了我以下结果:
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: 我想要得到的是MSFT和YHOO都与相应的信号和百分比变化对齐,如下所示:
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. 我尝试使用list(zip(returns,signal,pct_list)),但默认情况下,它似乎降至三个列表中的元素数量最少的一个,即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;
我想将ret_sig_list与一个for循环一起使用来对多只股票执行计算,以百分比变化上升或下降,以根据相关词语给出回报; 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!
科学,爱情,音乐等。我从Google Trends的Python软件包Pytrends中得到了这些单词,我建议这样做,它非常酷!
Use itertools.cycle
: 使用
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')]
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.
我说这不是理想的原因,因为其中不包含[“ SCIENCE”,“ LOVE”,“ MUSIC”]列表中的特定单词,但这是创建有序zip列表的唯一方法。
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. 接下来,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. 现在我们像这样从itertools导入周期。
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. 希望对任何对如何解决问题感到好奇的人有所帮助:)再次感谢PrestonM建议算法,以及Juapana帮助建议使用itertools循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.