Home > Mobile >  Is there any possible to save the output integer into array?
Is there any possible to save the output integer into array?

Time:11-22

everybody. I'm sorry if I look dumb at asking this question but I find this hard to complete my assignment.

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=' ')

My goal is that I want to get the Age output as an array and store it so that I can use it for the sorting algorithm. Is there any possible way to do this? Thanks!

CodePudding user response:

Just do ages.append(Age // 365) to collect all years into single list ages.

In following example I replaced Databaseprofile.get_all_profile(connection) with my own examples of dates, so that this example can be runnable easily by all StackOverflow users, without using any extra dependencies like database.

Try it online!

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)

Output:

4 19 31
[4, 19, 31]
  • Related