I want to increment a variable and – if a particular condition is fulfilled – I want to assign the next element of an iterator to it. In both cases the result should be appended to a list.
Problem is, that the function only recognizes the values already in the iterator.
The input-data is a nested list.
import datetime as dt
dates_prices = [[dt.datetime(2008, 6, 3, 0, 0), 48.54],
[dt.datetime(2008, 6, 6, 0, 0), 47.99]]
def fillDates(dates_prices):
filled = []
iter_data = iter(dates_prices)
item = iter_data.next()
filled.append(item)
while True:
item[0] += dt.timedelta(1)
try:
if item in dates_prices:
item = iter_data.next()
filled.append(item)
except StopIteration:
return filled
a = fillDates(dates_prices)
print a
The function should check, which dates are missing in the original nested list. It should add all missing dates together with the last known price-point, so the output shoud be this:
a =
[[dt.datetime(2008, 6, 3, 0, 0), 48.54],
[dt.datetime(2008, 6, 4, 0, 0), 48.54],
[dt.datetime(2008, 6, 5, 0, 0), 48.54],
[dt.datetime(2008, 6, 6, 0, 0), 47.99]]
What did I miss?
EDIT:
I altered the function that it is working now by creating a seperate list of dates from the nested list “dates_prices” and applying the suggestion by Sevenforce.
However, I still don’t know why my first solution didn’t work. I guess something with the variable assignment was wrong. But I don’t know what.
This is the new function:
import datetime as dt
dates_prices = [[dt.datetime(2008, 6, 3, 0, 0), 48.54], [dt.datetime(2008, 6, 6, 0, 0), 47.99]]
def fillDates(dates_prices):
filled = []
dates = [x[0] for x in dates_prices] #added this list
iter_data = iter(dates_prices)
item = iter_data.next()
filled.append(item[:])
while item[0] < dates[-1]:
item[0] += dt.timedelta(1)
if item[0] in dates: #using the new list here
item = iter_data.next()
filled.append(item[:]) #added colon here
return filled
a = fillDates(dates_prices)
print a
I am suspecting
dates_pricesis a nested list.You possibly want to add a copy of
itemtofilterand not the same object. To do this, change the linefilled.append(item)tofilled.append(item[:]). This will preventitem[0] += dt.timedelta(1)from changing already appended values in filled.To answer your edit:
Another
[:]was missing:iter_data = iter(dates_prices[:])prevents changes in inputdates_pricesitself (byitem[0] += dt.timedelta(1), btw this is still happening in your updated code). This led toif item in dates_pricesalways evaluate toTrue.With above change
if item in dates_priceswill be alwaysFalsesince[dt.datetime(2008, 6, 6, 0, 0), 48.54] != datetime.datetime(2008, 6, 6, 0, 0), 47.99]and therefore leads in an endless loop.Another working version (edited):
But there is still room for improvement, like using a dictionary for
dates_prices.@jsbueno:
You are right. The thing to learn here is to use something like
I think.