-1

我目前正在使用以下代码来获取两组财务数据的交集日期列。数组包括日期、o、h、l、cl

#find intersection of date strings
def intersect(seq1, seq2):
    res = []                     # start empty
    for x in seq1:               # scan seq1
        if x in seq2:            # common item?
            res.append(x)


    return res


x = intersect(seta[:,0], setb[:,0])    # mixed types
print x

问题是它只返回找到两者交集的列,即日期列。我希望它以某种方式返回一个不同的列数组,包括每个集合的 cls 值......即.. 如果日期是共同的,则两者都返回两个相应 cls 列的 2X1 数组。有任何想法吗?谢谢。

4

2 回答 2

0

好的,这是一个完整的解决方案。

获取一个python 库来下载股票报价

获取一些报价

start_date, end_date = '20090309', '20090720'
ibm_data = get_historical_prices('IBM', start_date, end_date)
msft_data = get_historical_prices('MSFT', start_date, end_date)

将行转换为字典的日期键字典

def quote_series(series):
    columns = ['open', 'high', 'low', 'close', 'volume']
    return dict((item[0], dict(zip(columns, item[1:]))) for item in series[1:])

ibm = quote_series(ibm_data)
msft = quote_series(msft_data)

做日期的交集

ibm_dates = set(ibm.keys())
msft_dates = set(msft.keys())

both = ibm_dates.intersection(msft_dates)

for d in sorted(both):
    print d, ibm[d], msft[d]
于 2009-07-22T20:31:33.933 回答
0

怎么样:

def intersect(seq1, seq2):
    if seq1[0] == seq2[0]:  # compare the date columns
        return (seq1[4], seq2[4])  # return 2-tuple with the cls values
于 2009-07-22T20:33:36.677 回答