Reputation: 11
I'm trying to take the /15 off the dates in this array. The array is ['6/03/15', '9/03/15', '10/03/15', '11/03/15', '12/03/15', '13/03/15', '16/03/15', '17/03/15', '18/03/15', '19/03/15'] and is named dateList.
def changeDateLength(dateList):
dateLength = dateList[0]
newList = []
for date in dateList:
if len(dateLength[0]) > 7:
shortDateLength = dateLength[:5]
else:
shortDateLength = dateLength[:4]
newList.append(shore)
return newList
the list prints out as ['6/03', '6/03', '6/03', '6/03', '6/03', '6/03', '6/03', '6/03', '6/03', '6/03']
Upvotes: 1
Views: 608
Reputation: 36106
The simplest solution (given quite strict assumptions about the format of the date) would be:
[d[:-3] for d in dateList]
resulting in:
['6/03', '9/03', '10/03', '11/03', '12/03', '13/03', '16/03', '17/03', '18/03', '19/03']
Upvotes: 0
Reputation: 91
Regular expressions are also a nice solution to the problem:
import re
dateList = ['6/03/15', '9/03/15', '10/03/15', '11/03/15', '12/03/15', '13/03/15', '16/03/15', '17/03/15', '18/03/15', '19/03/15']
result = [ i[:-3] for i in re.findall(r"\d{,2}/\d{,2}/15", str(dateList)) ]
print(result) # ['6/03', '9/03', '10/03', '11/03', '12/03', '13/03', '16/03', '17/03', '18/03', '19/03']
In case if you have dates with some other years that you do not want to touch:
dateList = ['8/12/14', '25/01/03', '6/03/15', '9/03/15', '10/03/15', '11/03/15', '12/03/15', '13/03/15', '16/03/15', '17/03/15', '18/03/15', '19/03/15']
result = [ i[:-3] if i in re.findall(r"\d{,2}/\d{,2}/15", str(dateList)) else i for i in dateList ]
print(result) # ['8/12/14', '25/01/03', '6/03', '9/03', '10/03', '11/03', '12/03', '13/03', '16/03', '17/03', '18/03', '19/03']
Upvotes: 0
Reputation: 10223
By List Comprehension :
Iterate every element from the given list and split element by /
and again join first two item from the split result by /
>>> l
['6/03/15', '9/03/15', '10/03/15', '11/03/15', '12/03/15', '13/03/15', '16/03/15', '17/03/15', '18/03/15', '19/03/15']
>>> ["/".join(i.split("/")[:2]) for i in l ]
['6/03', '9/03', '10/03', '11/03', '12/03', '13/03', '16/03', '17/03', '18/03', '19/03']
About Your Code:
Your Code:
def changeDateLength(dateList):
#- Why first item from the list is consider? This will raise exception IndexError
# when input is empty list.
# So not need to this.
dateLength = dateList[0]
#- Yes correct need new list varable.
newList = []
for date in dateList:
#- We iterate item from the list.
# so do process on item . dateLength[0] means first character from the dateLength variable which length is always 1.
# 1 > 7 will return False.
if len(dateLength[0]) > 7:
shortDateLength = dateLength[:5]
else:
shortDateLength = dateLength[:4]
#= Raise NameError exception because shore is not define
newList.append(shore)
return newList
Try:
def changeDateLength(dateList):
newList = []
for date_item in dateList:
if len(date_item) > 7:
shortDateLength = date_item[:5]
else:
shortDateLength = date_item[:4]
newList.append(shortDateLength)
return newList
dateList = ['6/03/15', '9/03/15', '10/03/15', '11/03/15', '12/03/15', '13/03/15', '16/03/15', '17/03/15', '18/03/15', '19/03/15']
new_dateList = changeDateLength(dateList)
print "new_dateList:", new_dateList
Upvotes: 3
Reputation: 7095
Since you're using dates, you can use the time
module to parse and format it.
import time
def strip_year(date):
return time.strftime('%-m/%d', time.strptime(date, '%d/%m/%y'))
Upvotes: 0
Reputation: 1111
Try the following simple list comprehension, we split by '/'
, take all up to the last item, and join the with a '/'
:
def changeDateLength(datelist):
return ['/'.join(item.split('/')[:-1]) for item in datelist]
>>> dateList = ['6/03/15', '9/03/15', '10/03/15', '11/03/15', '12/03/15', '13/03/15', '16/03/15', '17/03/15', '18/03/15', '19/03/15']
>>> changeDateLength(dateList)
['6/03', '9/03', '10/03', '11/03', '12/03', '13/03', '16/03', '17/03', '18/03', '19/03']
>>>
Upvotes: 0