You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python提取数组中2007年日期?现有提取2017年日期代码怎么改?

Hey there! Let's break down your two Python date-related questions step by step.


1. How to extract dates between 2007-01-01 and 2007-12-31 from an array?

You have two solid options here, depending on how consistent your date string formats are:

Option 1: Simple string comparison (for consistent formats)

If all your dates follow a YYYY-M-D or YYYY-MM-DD structure (year first, then month, then day), you can use direct string comparison—since this format’s lexicographical order matches actual date order. For extra simplicity, you can even just check if the date starts with 2007-:

dates = ["2006-12-31", "2007-1-1", "2007-6-15", "2007-12-31", "2008-1-1"]

# Filter dates that are in 2007
filtered_dates = [date for date in dates if date.startswith("2007-")]
print(filtered_dates)
# Output: ['2007-1-1', '2007-6-15', '2007-12-31']

Option 2: Use the datetime module (reliable for any valid format)

If your dates might have inconsistent formatting (like slashes instead of hyphens, or missing leading zeros), this is the way to go. It converts strings to proper date objects for accurate comparison:

from datetime import datetime

dates = ["2006-12-31", "2007-1-1", "2007/6/15", "2007-12-31", "2008-01-01"]
start_date = datetime(2007, 1, 1)
end_date = datetime(2007, 12, 31)

filtered_dates = []
for date_str in dates:
    # Adjust the format string to match your date structure (e.g., "%Y/%m/%d" for slashes)
    date_obj = datetime.strptime(date_str, "%Y-%m-%d")
    if start_date <= date_obj <= end_date:
        filtered_dates.append(date_str)

print(filtered_dates)
# Output: ['2007-1-1', '2007-12-31'] (note: the "2007/6/15" would work if we use "%Y/%m/%d")

2. Fixing your code to print only 2017 dates

The problem with your original code is that if item <= '2017-12-31' includes all dates on or before the end of 2017—including that 2015 date you don’t want. You need to narrow the condition to only include dates that fall within 2017. Here are two fixes:

Option 1: Check for the 2017 year prefix (quick fix for your current format)

Since all your dates use YYYY-M-D, just check if the string starts with 2017-:

base = ['2018-1-9', '2017-1-1', '2017-4-10', '2015-2-15', '2017-12-31', '2018-1-8', '2017-12-31', '2017-4-10', '2017-3-16']
for item in base:
    if item.startswith('2017-'):
        print(item)

This will output exactly the 2017 dates you want: 2017-1-1, 2017-4-10, 2017-12-31, 2017-12-31, 2017-4-10, 2017-3-16.

Option 2: Use datetime for robust comparison (scalable for future changes)

If you might work with different date formats later, this method avoids string-related edge cases:

from datetime import datetime

base = ['2018-1-9', '2017-1-1', '2017-4-10', '2015-2-15', '2017-12-31', '2018-1-8', '2017-12-31', '2017-4-10', '2017-3-16']
start_2017 = datetime(2017, 1, 1)
end_2017 = datetime(2017, 12, 31)

for item in base:
    date_obj = datetime.strptime(item, "%Y-%m-%d")
    if start_2017 <= date_obj <= end_2017:
        print(item)

This works even if your dates have leading zeros (like 2017-01-01) or other valid formats.


内容的提问来源于stack exchange,提问作者wen tian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:55:53