0
a = ['2023-02-10T00:00:35Z2023-02-13T00:00:00Z', '2023-02-06T09:03:24Z2023-02-13T00:00:00Z', '2023-02-11T19:27:55Z2023-02-18T19:00:00Z', '2023-02-12T18:00:00Z2023-02-13T04:00:00Z', '2023-02-12T18:00:51Z2023-02-19T00:00:00Z', '2023-02-12T20:00:00Z2023-02-13T01:00:00Z']

b = [['2023-02-10', '2023-02-11', '2023-02-12'], ['2023-02-10', '2023-02-11', '2023-02-12'], ['X', '2023-02-11', '2023-02-12'], ['X', 'X', '2023-02-12'], ['X', 'X', '2023-02-12'], ['X', 'X', '2023-02-12']]

I have two lists above, length of both the lists is always same and second list will always be a list of lists.

I would like to replace dates of list b with either 1, 0.5 or 0 based on the date time range given in list a.

First list of list b will correspond to first element of list a i.e date and time ranges , second list of list b will correspond to second element of list a and so on.

So I need to compare only nth element of list b (list) with nth element of list a (datetime range)

Desired output will be list b with below values.

b = [[1,1,1],[1,1,1],['X',0.5,1],['X','X',0.5],['X','X',0.5],['X','X',0.5]

Criteria : value 1 when whole 24 hours is completed for a given date; value 0.5 when < 24 hours and greater than 0 ; value 0 when 0 hours.

I am thinking of using slicing to compare dates from list a like below:

for l in b:
    for i,x in enumerate(l):
        for k in a:
            if x == k.split('T')[0]:
                if k.split('T')[1][:2] != '00':
                    ######

But I am not really sure whether this will work especially when I want to compare 1 to 1 elements.

Answers here states how to iterate over lists in parallel but how to iterate if one is list and another is a list of lists?

Any help would be appreciated.

Thanks.

Jazz365
  • 25
  • 1
  • 4
  • Use `zip()` to iterate over two lists in parallel, not nested loops. `for x, y in zip(a, b):` – Barmar Feb 12 '23 at 22:57
  • @Barmar Can you please tell how can I iterate if the other list is list of lists? – Jazz365 Feb 13 '23 at 14:25
  • Exactly as I showed. Why would a list of lists be any different? – Barmar Feb 13 '23 at 15:13
  • You can also use further spreading: `for x, (y1, y2, y3) in zip(a, b):`. And even `for i, (x, (y1, y2, y3)) in enumerate(zip(a, b)):` – Barmar Feb 13 '23 at 15:15
  • We can write `for x, (y1, y2, y3) in zip(a, b):` because we know there are 3 elements in list b. But what if we don't know how many elements(lists) are there in list b? – Jazz365 Feb 14 '23 at 22:04
  • 1
    You can use `for x, y in zip(a, b)` and then access the elements of `y` with indexes. – Barmar Feb 14 '23 at 22:05

0 Answers0