在第三方API函数中传递两个列表

时间:2018-06-04 12:52:24

标签: python python-3.x thomson-reuters-eikon

我已经被困在这个问题很多天了。

 a = pd.date_range(start= '02/02/2017', end='06/02/2018', freq = 'D')
 c = a.format(formatter=lambda x: x.strftime('%Y-%m-%d'))

    for date_to in c:
        date_to= date_to
        print("date_to has been picked up")
        b = pd.date_range(start= '02/01/2017', end='06/2/2018', freq = 'D')
        d = b.format(formatter=lambda x: x.strftime('%Y-%m-%d'))
        for date_from in d:
            date_from= date_from
            print('date_from has been picked up')
            df = ek.get_news_headlines('R:AAPL.O AND Language:LEN', date_from = date_from , date_to = date_to, count=100)

这是我编写的用于提取新闻形式第三方API的代码(在最后一行代码中)我面临的问题是,在最后一行我必须提供date_from和date_to来提供日期范围提取数据。现在我希望每次都像在循环中一样自动更改日期范围。最后一个循环" date_from"正在工作,但拳头循环没有提供" date_to"。提前感谢您的合作

1 个答案:

答案 0 :(得分:1)

您的问题有点不清楚,但我认为这是您想要实现的目标 - 同时获取成对日期并转移到下一对(n次迭代),而不是两个嵌套for循环(n 2 迭代):

#Create date lists first
a = pd.date_range(start= '02/02/2017', end='06/02/2018', freq = 'D')
b = pd.date_range(start= '02/01/2017', end='06/2/2018', freq = 'D')
c = a.format(formatter=lambda x: x.strftime('%Y-%m-%d'))
d = b.format(formatter=lambda x: x.strftime('%Y-%m-%d'))

# Single for loop iterating over pairs of elements of c,d
for date_to,date_from in zip(c,d): 
    print("date_to has been picked up")
    print('date_from has been picked up')
    df = ek.get_news_headlines('R:AAPL.O AND Language:LEN', date_from = date_from , date_to = date_to, count=100)

您可能想了解zip()的工作原理:

https://docs.python.org/3.4/library/functions.html#zip