I'm very new to programming. I've tried to search the website for similar problems, but can't find the information I need.
I have a list that contains multiple strings showing year, month, day and hour. I need to split this list into four lists of years, months, days and hours. The values have to be integers in the four lists.
The time format is: 'dd.mm.yyyy hh', example: '01.11.2020 02'
I'm able to split the string '01.11.2020 02' using this code:
timeStamp = '01.11.2020 02'
def getYear(timeStampStr):
yearStr = timeStampStr[6:10]
year = int(yearStr)
return year
def getMonth(timeStampStr):
monthStr = timeStampStr[3:5]
month = int(monthStr)
return month
def getDay(timeStampStr):
dayStr = timeStampStr[0:2]
day = int(dayStr)
return day
def getHour(timeStampStr):
hourStr = timeStampStr[11:13]
hour = int(hourStr)
return hour
I can then get the wanted result with:
print(getMonth(timeStamp))
However, this doesnt work when timeStamp is a list;
timeStamp = ['01.11.2020 00:00', '01.11.2020 01:00', '01.11.2020 02:00', etc].
What can I do to split it into four?
CodePudding user response:
For list type you can use map
like this
timeStamp = ['01.11.2020 00:00', '01.11.2020 01:00', '01.11.2020 02:00']
print(list(map(getMonth, timeStamp)))
CodePudding user response:
you can also use split fn to divide the string and then label them timeStamp.split('.')