簡體   English   中英

如何通過函數運行日期字符串列表並將每個項目的結果作為一個串聯字符串返回?

[英]How can I run a list of date strings through a function and return the results of each item as one concatonated string?

我需要編寫一個程序來驗證給定日期的列表。 驗證列表中的每個日期都需要返回“-”(如果有效)或年份的結束數字(如果無效)。 這些返回需要作為一個字符串附加到 'Results = ""' 以完成問題。

所以我取消了日期驗證,但現在我一直在試圖弄清楚如何通過我的驗證程序正確運行列表程序,以便返回每個項目以創建一個新的結果字符串

到目前為止,我有這個:

def ListofDates (): #this is the program my teach gave us. The validation included seemed confusing to me.
    DateList = []
    for mon in range (1,13):
        Yr,Feb = 2001,0
        if (mon == 2):
            Feb = 2
            if Yr%400==0 or Yr%100!=0 and Yr%4==0:  # if leap year, make Feb subtract one less day
                Feb = 1
        MonSt = "{0:0>2}".format(mon)
        DateList = DateList + [MonSt +"/00/"+str(Yr)] + [MonSt +"/01/"+str(Yr)]
        ShortMon = (mon-1)%7%2 # (teacher comment) Equals 1 or 30 (or less) day months and 0 for 31 day months
        MaxDay = 31 - ShortMon - Feb
        DateList = DateList + [MonSt +"/"+ str(MaxDay+1) +"/"+ str(Yr)] + [MonSt +"/"+ str(MaxDay) +"/"+str(Yr)]
    DateList = DateList + ["02/29/1900"] + ["02/29/2000"]
    DateList = DateList + ["00/29/2000"] + ["02/29/2400"]
    DateList = DateList + ["13/29/2000"] + ["12/31/2500"]
    DateList = DateList + ["01/01/2501"] + ["02/28/0001"]
    DateList = DateList + ["02/28/0000"] + ["02/29/400"]
    return (DateList)

def dayMonth_is_valid(month,day,year):
    if month in [1,3,5,7,8,10,12]:
        return 1 <= day <= 31
    if month in [4,6,9,11]:
        return 1 <= day <= 30
    if month == 2:
        if year % 400 == 0:
            return 1<=day<=29
        if year % 4 == 0:
            if year % 100 == 0:
                return 1 <= day <= 28
            else:
                return 1 <= day <= 29
        else:
            return 1<= day <=28

def year_is_valid(year):
    return 1 <= year <=2500

def date_is_valid(date):
    month,day,year = map(int, date.split("/"))

    if dayMonth_is_valid(month,day,year) and year_is_valid(year)==True:
        print ("-")
    else:
        print (str(year%10)) #is there a better way to do this?

def validate_date_list():
    dates = ListofDates()
    Results = ""
    for date in dates:
        Results.append (date_is_valid(date))

validate_date_list()

我實際上沒有完成 if 的結果部分,因為我什至無法獲得運行驗證程序的日期。 我也知道不能附加字符串。 我想我會把結果列成一個列表,然后附加它然后加入它(但這似乎有很多步驟)

所以我的驗證程序工作正常,但我不知道如何通過它運行該列表。 這和格式有關系嗎? “日期”是作為一個列表開始閱讀的嗎?

另外,對於返回的“-”或 str(year%10): 這是完成此步驟的最佳位置嗎? 或者我應該返回真/假然后在主函數中生成字符串?

如何通過“date_is_valid()”運行“dates”列表中的每個單獨項目作為完成該功能的適當變量?

我查看了列表理解和 for 語句的不同方法,但無法弄清楚。

您需要return值而不是打印它們。 此外,您可以使用str[-1]來返回年份的結束數字而不是%10

def date_is_valid(date):

    month,day,year = map(int, date.split("/"))
    
    if dayMonth_is_valid(month,day,year) and year_is_valid(year)==True:
        return ("-")
    else:
        return (str(year)[-1])

def validate_date_list():
    dates = ListofDates()
    Results = ""
    for date in dates:
        Results += date_is_valid(date)

validate_date_list()

最好從date_is_valid返回 True 或 False 然后將join方法與生成器一起使用:

dates = ListofDates()
results = ''.join('-' if date_is_valid(date) else date[-1] for date in dates)

順便說一下, expression == True是絕對沒有必要的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM