0

I have the following list "diasformated":

  ['Wednesday, 2023-02-01', 'Thursday, 2023-02-02', 'Friday, 2023-02-03', 'Saturday, 2023-02-04', 'Monday, 2023-02-06', 'Tuesday, 2023-02-07', 'Wednesday, 2023-02-08', 'Thursday, 2023-02-09', 'Friday, 2023-02-10', 'Saturday, 2023-02-11', 'Monday, 2023-02-13', 'Tuesday, 2023-02-14', 'Wednesday, 2023-02-15', 'Thursday, 2023-02-16', 'Friday, 2023-02-17', 'Saturday, 2023-02-18', 'Monday, 2023-02-20', 'Tuesday, 2023-02-21', 'Wednesday, 2023-02-22', 'Thursday, 2023-02-23', 'Friday, 2023-02-24', 'Saturday, 2023-02-25', 'Monday, 2023-02-27', 'Tuesday, 2023-02-28']

I want to convert that into this type of list of lists, in such a way each sublist represent a week of that month:

[['Wednesday_2023_02_01',
  'Thursday_2023_02_02',
  'Friday_2023_02_03',
  'Saturday_2023_02_04',
  ['Monday_2023_02_06',
  'Tuesday_2023_02_07',
 'Wednesday_2023_02_08',
  'Thursday_2023_02_09',
  'Friday_2023_02_10',
  'Saturday_2023_02_11']
  ['Monday_2023_02_13',
  'Tuesday_2023_02_14'...,

but I get this result (it's returned a out of range error but you can type the name of the list and will return you this):

  [['Wednesday_2023_02_01',
  'Thursday_2023_02_02',
  'Friday_2023_02_03',
  'Saturday_2023_02_04',
  'Monday_2023_02_06',
  'Tuesday_2023_02_07',
  'Wednesday_2023_02_08',
  'Thursday_2023_02_09',
  'Friday_2023_02_10',
  'Saturday_2023_02_11',
  'Monday_2023_02_13',
  'Tuesday_2023_02_14',
  'Wednesday_2023_02_15',
  'Thursday_2023_02_16',
  'Friday_2023_02_17',
  'Saturday_2023_02_18',
  'Monday_2023_02_20',
  'Tuesday_2023_02_21',
  'Wednesday_2023_02_22',
  'Thursday_2023_02_23',
  'Friday_2023_02_24',
  'Saturday_2023_02_25',
  'Monday_2023_02_27',
  'Tuesday_2023_02_28'],
 [],
 [],
 [],
 []]

Product of this code which is missing something that I don't know:

ndays=end_dt-start_dt
weeks = calendar.monthcalendar(year, month_num)
nweeks = len(weeks)
turnlist=[[] for e in range(0,nweeks)]

#bring the days in n weeks (before was 4)
conteo1=0
for i in range(len(turnlist)):
    for j in range(len(diasformated)):
        while len(turnlist[i])<6 or 'Monday' not in diasformated[j]:
            turnlist[i].append(diasformated[j+conteo1])
            conteo1+=1 

How can I get the desired result? I appreciate the help!

Arnoldo Oliva
  • 103
  • 1
  • 9
  • Get out of the habit of using `for index in range(len(list)):`. Use `for item in list:` or `for index, item in enumerate(list):` – Barmar Dec 29 '22 at 22:57
  • When I run your code I get a "list index out of range" error. – Barmar Dec 29 '22 at 23:04
  • The error happens when `i == 0` `j == 0`, and `conteo1 == 24` – Barmar Dec 29 '22 at 23:07
  • I'll edit the question, it returns you the out of range error but if you see the output you can see a list. – Arnoldo Oliva Dec 29 '22 at 23:08
  • When I look at the partial output that was created before the error, it doesn't look like yours. Everything is in the first sublist of `turnlist`, the other 4 are empty. – Barmar Dec 29 '22 at 23:09
  • I've just updated the post fixing that, thanks for letting me know – Arnoldo Oliva Dec 29 '22 at 23:10
  • I think this question should be helpful: https://stackoverflow.com/questions/3806473/week-number-of-the-month Get the week in the month, then use that as the index into `turnlist`. – Barmar Dec 29 '22 at 23:14

0 Answers0