从python

时间:2015-05-13 05:48:59

标签: python

我正在尝试将此数组中的日期取消/ 15。阵列是['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']并命名为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

列表打印出['6/03','6/03','6/03','6/03','6/03','6/03','6/03' ,'6/03','6/03','6/03']

5 个答案:

答案 0 :(得分:3)

列表理解

迭代给定列表中的每个元素,拆分元素/再次加入分割结果中的前两项按/

>>> 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']

关于您的代码

您的密码:

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

尝试:

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

答案 1 :(得分:0)

尝试以下简单列表理解,我们按'/'拆分,直到最后一项,并加入'/'

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']
>>> 

答案 2 :(得分:0)

由于您正在使用日期,因此可以使用time模块对其进行解析和格式化。

import time

def strip_year(date):
    return time.strftime('%-m/%d', time.strptime(date, '%d/%m/%y'))

答案 3 :(得分:0)

正则表达式也是解决问题的好方法:

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']

如果您有其他年份的日期,您不想触摸:

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']

答案 4 :(得分:0)

最简单的解决方案(对日期格式给出非常严格的假设)将是:

[d[:-3] for d in dateList]

导致:

['6/03', '9/03', '10/03', '11/03', '12/03', '13/03', '16/03', '17/03', '18/03', '19/03']