0

With the following code:

class Calendar_Data(Resource):
  def get(self):
    result = []
    details_array = []
    # Times are converted to seconds
    for day in life.days:
      for span in day.spans:
        if type(span.place) is str:
          details = {
            'name': span.place,
            'date': 0,
            'value': (span.length() * 60),
          }
          details_array.append(details)
      data = {
        'date': datetime.datetime.strptime(day.date, '%Y_%m_%d').strftime('%Y-%m-%d'),
        'total': (day.somewhere() * 60),
        'details': details_array
      }
      result.append(data)
    return result

What I'm trying to do is for each day that is present in a list of days, get the corresponding spans for that day, and fill the array with the details. Then pass that details to the data array in order to have it for each day of that list of days.

The problem here is when I use these nested loops above, it fills me the details with the all the spans from all days, instead of each single day.

I don't thin using a zip in this case would work. Maybe some list comprehension but I still did not understand that fully.

Example input:

--2016_01_15
@UTC
0000-0915: home
0924-0930: seixalinho station
1000-1008: cais do sodre station
1009-1024: cais do sodre station->saldanha station
1025-1027: saldanha station
1030-1743: INESC
1746-1750: saldanha station
1751-1815: saldanha station->cais do sodre station
1815-1834: cais do sodre station {Waiting for the boat trip back. The boat was late}
1920-2359: home [dinner]

--2016_01_16
0000-2136: home
2147-2200: fabio's house
2237-2258: bar [drinks]

For the sixteenth of january the details array is supposed to have 3 items, but each day constantly shows all of the items of all days.

2
  • Can you please add an example of your input, your expected output and your actual output. Commented Jul 26, 2016 at 23:08
  • @IanAuld I've added it Commented Jul 26, 2016 at 23:13

1 Answer 1

1

You aren't redeclaring your list (Python has lists not arrays) in between each loop. You need to move the creation of your details_array inside one of the loops so it is recreated as empty. You'll likely wnt it to look like this:

for day in life.days:
    details_array = []
    for span in day.spans:

This way for each new iteration of a day you'll have a new empty list.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.