Home > OS >  Iterate through two lists in python
Iterate through two lists in python

Time:02-16

I have two different lists, one has a set of days (70) and the other has 3 names. I would like to assign a name to every 7 days, and thought this was the way to do so, but now it only assigns one name to every date.

date_list is a list of 70 days and names contains 3 different names.

How can I fix this?

    date_dict = {}
    sum_names = len(names)
    counter = 0

for date in date_list:
                # If counter is sum of names, reset counter to 0
                if (counter == sum_names):
                        counter = 0

                # Else increment counter and add dictionary key/value        
                else:
                        date_dict[date] = names[counter]
                        counter  = 1


print(date_dict)

CodePudding user response:

By using extended slicing you can split your list into every nth interval (ie every seventh day).

In your case this can be done in the following way:

every_seventh_day = date_list[::7]
>> [day0, day7, day14, etc..]

You can then adjust your code in the following way:

for date in every_seventh_day:
                # If counter is sum of names, reset counter to 0
                if (counter == sum_names):
                        counter = 0

                date_dict[date] = names[counter]
                counter  = 1

ps. The current implementation of the else statement will skip the iteration of counter=0 and names[0] will never be assigned a date. - unless this is intended for your use case it should be safe to remove :)

You can read more about slicing from the docs, specifically this example. https://python-reference.readthedocs.io/en/latest/docs/brackets/slicing.html#example-3

CodePudding user response:

Having two counters for keeping track of the dates and names should work.

    date_dict = {}
    sum_names = len(names)
    date_len = 7
    date_counter = 0
    name_counter = 0

for date in date_list:
                # If date counter has hit 7 days then reset date counter and increment name_counter
                if (date_counter == date_len):
                        date_counter = 0
                        #if the name_counter has hit the sum of names then reset else increment
                        if(name_counter == sum_names):
                          name_counter=0
                        else:
                          name_counter =1

                # Else increment counter and add dictionary key/value        
                else:
                        date_dict[date] = names[name_counter]
                        date_counter  = 1

CodePudding user response:

If you take your list of three names and turn it into a list of 3x7 names, you can just cycle over it.

So starting with:

names = ['Donald', 'Ricky', 'Morty']

# Three * 7 names:
groups = [word for entry in names for word in [entry]*7]
# ['Donald','Donald','Donald',...'Ricky', 'Ricky', ... 'Morty']

With that you can just zip the cycle and dates:

from datetime import datetime, timedelta
from itertools import cycle

# a list of 70 dates
today = datetime.today()
date_list = [(today   timedelta(days=x)).strftime('%Y-%m-%d') for x in range(70)]

names = ['Donald', 'Ricky', 'Morty']
groups = [word for entry in names for word in [entry]*7]

# zip with cycle for a dict (or any other structure you want):
{date: name for date, name in zip(date_list, cycle(groups))}

Which will give you:

{'2022-02-15': 'Donald',
 '2022-02-16': 'Donald',
 '2022-02-17': 'Donald',
 '2022-02-18': 'Donald',
 '2022-02-19': 'Donald',
 '2022-02-20': 'Donald',
 '2022-02-21': 'Donald',
 '2022-02-22': 'Ricky',
 '2022-02-23': 'Ricky',
 '2022-02-24': 'Ricky',
 '2022-02-25': 'Ricky',
 ...
 '2022-04-17': 'Morty',
 '2022-04-18': 'Morty',
 '2022-04-19': 'Donald',
 '2022-04-20': 'Donald',
 '2022-04-21': 'Donald',
 '2022-04-22': 'Donald',
 '2022-04-23': 'Donald',
 '2022-04-24': 'Donald',
 '2022-04-25': 'Donald'}
  • Related