簡體   English   中英

在自定義日期對象列表中查找最早的日期

[英]Finding the earliest date in list of custom date objects

給定日期對象列表,我需要找到文件中最早出現的日期

我編寫了以下代碼來執行此操作:

return min(dates, key=lambda x: x.year)

每個對象都有一個年、月和日屬性。 我必須使用我創建的自定義類(在帖子底部)

此代碼在同一年有兩個日期之前一直有效。 如果兩個日期具有相同的年份,它會返回列表中第一個出現的日期,這不是預期的。

例如,這兩個日期:

1988 10 10
1988 09 03

該函數應該返回 1988 09 03 ,但它首先返回 1988 10 10 。

如何在python中比較一個對象的多個值?

我也試過這個

return min(min(min(dates, key=lambda x: x.year), key=lambda x: x.month), key=lambda x: x.day)

但它似乎不起作用

我也試過

return min(dates)

但這只是返回列表中的最后一個日期

日期文件示例:

1954 10 04
1987 12 20
1993 3 10
1996 7 29
1994 5 30
1993 6 9
1989 12 21
2001 6 30
1995 6 14
2002 2 28
1988 6 21
class Date(object):
    def __init__(self, year, month=1, day=1):
        self.year = year
        self.month = month
        self.day = day

    def __str__(self):
        return '{}/{}/{}'.format(self.year, self.month, self.day)

    def __lt__(self, other):
        return (self.year, self.month, self.day) < (other.year, other.month, other.day)

    def __gt__(self, other):
        return (self.year, self.month, self.day) > (other.year, other.month, other.day)

    def __eq__(self, other):
        return (self.year, self.month, self.day) == (other.year, other.month, other.day)

    def __le__(self, other):
        return (self.year, self.month. self.day) <= (other.year, other.month, other.day)

    def __ge__(self, other):
        return (self.year, self.month, self.day) >= (other.year, other.month, other.day)

    def __ne__(self, other):
        return (self.year, self.month, self.day) != (other.year, other.month, other.day)

解析:

def parse_dates(content):
    dates = []
    for line in content:
        line_date = line.strip().split()
        dates.append(Date(line_date[0], line_date[1], line_date[2]))

    return dates

該文件被簡單地打開,然后通過它返回一個日期對象列表

然后,將其傳遞給 this 以返回最早的日期:

def find_earliest(dates):
    return min(dates)
The file where the dates are retrieved from is shown above

解決了我沒有將日期轉換為 int 所以它們保持為字符串,因此程序比較的是字符串而不是實際的數值。 我只是將所有內容都轉換為 int 然后創建了日期對象。

如果您沒有在Date構造函數中強制轉換為int ,則需要在解析中進行:

def parse_dates(content):
    dates = []
    for line in content:
        line_date = line.strip().split()
        dates.append(Date(*map(int, line_date)))

    return dates

或作為單線:

def parse_dates(content):
    return [Date(*map(int, line.strip().split())) for line in content]

如果你不想定義所有的 dunder 比較方法,你可以使用functools.total_ordering

from functools import total_ordering

@total_ordering
class Date(object):
    def __init__(self, year, month=1, day=1):
        #cast your values to ints
        self.year  = int(year)
        self.month = int(month)
        self.day   = int(day)

    def __str__(self):
        # using f-string and padding
        # to get "1994/02/07" instead of "1994/2/7"
        return f'{self.year}/{self.month:02}/{self.day:02}'

    def __eq__(self, other):
        return self.year  == other.year
           and self.month == other.month
           and self.day   == other.day

    def __lt__(self, other):
        # works because `or` short-circuits on first True
        return self.year  < other.year
            or self.month < other.month
            or self.day   < other.day

暫無
暫無

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

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