大家。如果我問這個問題看起來很愚蠢,我很抱歉,但我發現這很難完成我的任務。
profile = Databaseprofile.get_all_profile(connection)
for prof in profile:
date = prof[2]
datem = datetime.datetime.strptime(date, "%Y-%m-%d")
tod = datem.day
mos = datem.month
yr = datem.year
today_date = datetime.datetime.now()
dob = datetime.datetime(yr, mos, tod)
time_diff = today_date - dob
Age = time_diff.days
#Databaseprofile.insertionsort(Age)
print(Age//365, end=' ')
我的目標是將 Age 輸出作為陣列獲取并存盤,以便我可以將其用于排序演算法。有沒有可能的方法來做到這一點?謝謝!
uj5u.com熱心網友回復:
只需ages.append(Age // 365)將所有年份收集到單個串列中即可ages。
在下面的示例中,我替換Databaseprofile.get_all_profile(connection)了我自己的日期示例,以便所有 StackOverflow 用戶都可以輕松運行此示例,而無需使用任何額外的依賴項,如資料庫。
在線試試吧!
import datetime
# profile = Databaseprofile.get_all_profile(connection)
profile = [[None, None, e] for e in ['2017-09-16', '2002-04-05', '1990-06-01']]
ages = []
for prof in profile:
date = prof[2]
datem = datetime.datetime.strptime(date, "%Y-%m-%d")
tod = datem.day
mos = datem.month
yr = datem.year
today_date = datetime.datetime.now()
dob = datetime.datetime(yr, mos, tod)
time_diff = today_date - dob
Age = time_diff.days
#Databaseprofile.insertionsort(Age)
ages.append(Age // 365)
print(ages[-1], end = ' ')
print()
print(ages)
輸出:
4 19 31
[4, 19, 31]
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/362829.html
