任務:我必須生成一些隨機的出生日期并將其添加到我的三個字典串列中。我已經生成了隨機任務,但我堅持將“出生日期”鍵添加到鴨子內的字典中,并使用我創建的串列中的值。有什么建議嗎?這是我到目前為止的代碼:
ducks =[{'first_name': 'Davey', 'last_name': 'McDuck', 'location': "Rob's Office", 'insane': True, 'followers': 12865, 'following': 120,
'weapons': ['wit', 'steely stare', 'devilish good looks'], 'remorse': None},
{'first_name': 'Jim', 'last_name': 'Bob', 'location': 'Turing Lab', 'insane': False, 'followers': 123,
'following': 5000, 'weapons': ['squeak'], 'remorse': None},
{'first_name': 'Celest', 'last_name': '', 'location': 'Throne Room', 'insane': True, 'followers': 40189, 'following': 1,
'weapons': ['politics', 'dance moves', 'chess grandmaster', 'immortality']}] #list with three dictionaries
import random
dob = []
def dateofbirth(number=1):
Year = random.randrange(1990, 2010)
for item in range(number):
yield random.randrange(1990, 2010), random.randrange(1, 12), random.randrange(1, 30)
dateTimeThatIwant = dateofbirth(3)
#print(dateTimeThatIwant)
for year, month, date in dateTimeThatIwant:
#print((year, month, date))
dob.append([year, month, date])
print(dob)
for d in ducks:
d["dob"] = dob_value
uj5u.com熱心網友回復:
在這里,您可以zip用來遍歷兩個容器。它將允許您采用兩個可迭代物件并將其作為元組回傳。這些值可以簡單地用于普通的 for 回圈。你應該注意到,這種假設都ducks與dob具有相同的大小。
ducks =[{'first_name': 'Davey', 'last_name': 'McDuck', 'location': "Rob's Office", 'insane': True, 'followers': 12865, 'following': 120,
'weapons': ['wit', 'steely stare', 'devilish good looks'], 'remorse': None},
{'first_name': 'Jim', 'last_name': 'Bob', 'location': 'Turing Lab', 'insane': False, 'followers': 123,
'following': 5000, 'weapons': ['squeak'], 'remorse': None},
{'first_name': 'Celest', 'last_name': '', 'location': 'Throne Room', 'insane': True, 'followers': 40189, 'following': 1,
'weapons': ['politics', 'dance moves', 'chess grandmaster', 'immortality']}] #list with three dictionaries
import random
dob = []
def dateofbirth(number=1):
Year = random.randrange(1990, 2010)
for item in range(number):
yield random.randrange(1990, 2010), random.randrange(1, 12), random.randrange(1, 30)
dateTimeThatIwant = dateofbirth(3)
#print(dateTimeThatIwant)
for year, month, date in dateTimeThatIwant:
#print((year, month, date))
dob.append([year, month, date])
print(dob)
for c_dob, d in zip(dob, ducks):
d['dob'] = c_dob
uj5u.com熱心網友回復:
如果不使用生成器并允許鴨子串列具有任何長度并且使用僅生成有效日期的偽隨機出生日期生成器,您可以這樣做:
from datetime import datetime
from random import randint
ducks = [{'first_name': 'Davey', 'last_name': 'McDuck', 'location': "Rob's Office", 'insane': True, 'followers': 12865, 'following': 120,
'weapons': ['wit', 'steely stare', 'devilish good looks'], 'remorse': None},
{'first_name': 'Jim', 'last_name': 'Bob', 'location': 'Turing Lab', 'insane': False, 'followers': 123,
'following': 5000, 'weapons': ['squeak'], 'remorse': None},
{'first_name': 'Celest', 'last_name': '', 'location': 'Throne Room', 'insane': True, 'followers': 40189, 'following': 1,
'weapons': ['politics', 'dance moves', 'chess grandmaster', 'immortality']}]
def randomdate(loyear=1990, hiyear=2010):
while True:
try:
yy = randint(loyear, hiyear)
mm = randint(1, 12)
dd = randint(1, 31)
datetime.strptime(f'{dd}/{mm}/{yy}', '%d/%m/%Y')
return [yy, mm, dd]
except ValueError:
pass
for duck in ducks:
duck['dob'] = randomdate()
print(ducks)
萬一年、月和日的隨機選擇產生無效的結果,datetime.strptime 將引發 ValueError,所以我們只需再試一次
uj5u.com熱心網友回復:
ducks =[{'first_name': 'Davey', 'last_name': 'McDuck', 'location': "Rob's Office", 'insane': True, 'followers': 12865, 'following': 120,
'weapons': ['wit', 'steely stare', 'devilish good looks'], 'remorse': None},
{'first_name': 'Jim', 'last_name': 'Bob', 'location': 'Turing Lab', 'insane': False, 'followers': 123,
'following': 5000, 'weapons': ['squeak'], 'remorse': None},
{'first_name': 'Celest', 'last_name': '', 'location': 'Throne Room', 'insane': True, 'followers': 40189, 'following': 1,
'weapons': ['politics', 'dance moves', 'chess grandmaster', 'immortality']}] #list with three dictionaries
import random
dob = []
def dateofbirth(number=1):
Year = random.randrange(1990, 2010)
for item in range(number):
yield random.randrange(1990, 2010), random.randrange(1, 12), random.randrange(1, 30)
dateTimeThatIwant = dateofbirth(3)
#print(dateTimeThatIwant)
for year, month, date in dateTimeThatIwant:
#print((year, month, date))
dob.append([year, month, date])
print(dob)
for i,d in enumerate(ducks):
d["dob"]=d.get("dob",dob[i])
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/328991.html
上一篇:根據物件id更新串列中的物件
下一篇:Numpy函式引數混淆串列或元組
