简体   繁体   中英

Combining list of different lengths and operating on the elements

I have two lists of dates "p_dates" and "a_dates". The simplest case will be with p_dates of n elements and a_dates of n+1 elements. I wish to build a Table o_table(0 to n, 0 to 3) that looks like this:

0,p_dates[0], a_dates[0], a_dates[1],(a_dates[1]-a_dates[0])/365
...
...
n, p_dates[n], a_dates[n], a_dates[n+1],(a_dates[n+1]-a_dates[n])/365

Is there a more pythonic way of obtaining the above output other than using the obvious loop while solution?

zip(itertools.count(0),p_dates,a_dates,a_dates[1:])

maybe i guess ... Im not sure what p_dates and a_dates look like ...

this really only gets you started ...

from itertools import izip,count
result = [(a,b,c,d,(d-c)/360.0)for a,b,c,d in izip(count(0),p_dates,a_dates,a_dates[1:])]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM