[Tutor] suggestions for splitting file based on date

Peter Otten __peter__ at web.de
Sat Jul 20 11:11:17 CEST 2013


Sivaram Neelakantan wrote:

> On Sat, Jul 20 2013,Peter Otten wrote:
> 
>> Sivaram Neelakantan wrote:
> 
> [snipped 16 lines]
> 
>> I'd start with a single list for the complete data, reverse that using
>> the aptly named method and then create the three smaller lists using
>> slicing.
>>
>> For example:
>>
>>>>> stock_data = range(10)
>>>>> stock_data.reverse()
>>>>> stock_data
>> [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
>>>>> stock_data[:3] # the last three days
>> [9, 8, 7]
>>
>> On second thought I don't see why you want to reverse the data. If you
>> omit that step you need to modify the slicing:
>>
>>>>> stock_data = range(10)
>>>>> stock_data[-3:] # the last three days
>> [7, 8, 9]
>>
> 
> Thank you, this is as simple as it gets.  The only wiggle is the
> trading day versus the calendar day.  If I use the trading day, the
> above slicing will work(a simple numerical count, to represent 30
> *trading* day window) but the calendar day is a lot more hops
> accounting for weekends or days of no trading.  I believe I'd have to
> end up doing a date compare.

You could insert dummy items into the list for days with no trading.

Alternatively, use bisect() to determine the list index, see

http://docs.python.org/2.7/library/bisect.html



More information about the Tutor mailing list