這個問題是從超級用戶遷移過來的,因為它可以在 Stack Overflow 上回答。 昨天遷移 。
我有兩個不同的串列,一個有一組天(70),另一個有 3 個名稱。我想為每 7 天分配一個名稱,并認為這是這樣做的方法,但現在它只為每個日期分配一個名稱。
date_list 是 70 天的串列,名稱包含 3 個不同的名稱。
我怎樣才能解決這個問題?
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)
uj5u.com熱心網友回復:
如果你把三個名字的串列變成一個 3x7 名字的串列,你可以回圈遍歷它。
所以開始:
names = ['Donald', 'Ricky', 'Morty']
# Three * 7 names:
groups = [word for entry in names for word in [entry]*7]
# ['Donald','Donald','Donald',...'Ricky', 'Ricky', ... 'Morty']
有了它,您可以壓縮周期和日期:
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))}
這會給你:
{'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'}
uj5u.com熱心網友回復:
有兩個計數器來跟蹤日期和名稱應該可以作業。
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
uj5u.com熱心網友回復:
通過使用擴展切片,您可以將串列拆分為每第 n 個間隔(即每第七天)。
在您的情況下,可以通過以下方式完成:
every_seventh_day = date_list[::7]
>> [day0, day7, day14, etc..]
然后,您可以通過以下方式調整代碼:
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
附言。該else陳述句的當前實作將跳過 counter=0 的迭代,并且永遠不會為 names[0] 分配日期。- 除非這是為您的用例設計的,否則應該可以安全洗掉:)
您可以從檔案中閱讀更多關于切片的資訊,特別是這個例子。 https://python-reference.readthedocs.io/en/latest/docs/brackets/slicing.html#example-3
編輯:
解決方案2:以下解決方案每天回圈逐步減小date_list大小直到為空,
在每次迭代中,它會date_dict在接下來的 7 天內從date_listas 鍵和name[counter]as 值進行更新。
然后它增加名稱計數器值并檢查是否需要重置。此外,還要檢查是否有 7 天供下一個人使用,其余的則給他們。
date_dict = {}
date_list= ["2022-02-16", "2022-02-17", "2022-02-18", etc..]
names = ["Donald", "Rick"]
counter = 0
while len(date_list) > 0:
date_dict.update(dict.fromkeys([date_list.pop(0) for _ in range(7)], names[counter]))
counter =1
if counter == len(names):
counter = 0
# Safety check if the list doesnt have at least 7 dates for the next iteration
if len(date_list) < 7:
date_dict.update(dict.fromkeys([date_list.pop(0) for _ in range(len(date_list))], names[counter]))
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/425637.html
標籤:Python python-3.x 列表 字典
下一篇:使用索引訪問串列時出現索引錯誤
