I had an np.array contains a set of string (with different length each one) like this example:
title=['the first step in 2017', 'Here is my 2016 report', '2016 new considerations' ....]
I want to extract the year from each element in the array I had written this piece of code :
list_yea=[]
for i, tit in enumerate(title) :
if '20' in tit:
print(year)# ??? I could not find a best solution
list_yea.append(year)
I assumed that all the years are within the range [2000-2020] My problem is how to return only the year from that string
I have tried this code but it gave me wrong result:
years=[]
c=1 # tocheck the number of string does not contain the year
for i, tit in enumerate(title) :
if '20' in tit or '199' in tit : # for both 199x and 20xx years
spl=tit.split(' ')
for j , check in enumerate(spl):
if '20' in check:
years.append(check)
if '20' not in tit and '199' not in tit :
c=c+1
years.append(0)
len(years) ==> 16732
While my total dataset was 16914 samples
Thank you in advance for any help