【问题标题】:Is there's a way to print only first and last value in loop when given condition become true?当给定条件为真时,有没有办法只打印循环中的第一个和最后一个值?
【发布时间】:2025-11-29 06:45:01
【问题描述】:

大家好,我正在做一个项目,我只想在条件为真时打印给定条件的第一个和最后一个值...

from astropy.time import Time
import swisseph as swe
from datetime import timedelta as td, datetime

swe.set_ephe_path('G:\Ephemeris')
swe.set_jpl_file('de440.eph')

start_date = '2021-01-01 00:00:00'
end_date = '2021-12-31 00:00:00'
d1 = datetime.strptime(start_date, '%Y-%m-%d %H:%M:%S')
d2 = datetime.strptime(end_date, '%Y-%m-%d %H:%M:%S')

def get_delta(d1, d2):
    delta = d2 - d1
    return delta

delta = get_delta(d1,d2)

for i in range(delta.days * 1 + 1):
    x = d1 + td(days=i)
    jd = Time(x).jd
    eph = swe.calc_ut(jd, 2, swe.FLG_SPEED+swe.FLG_JPLEPH)[0][3]
    if eph < 0:
        print(x, eph)

输出---

2021-01-31 00:00:00 -0.06355796829817034    <--- Condition true print this value (first value)
2021-02-01 00:00:00 -0.2516828089143771
2021-02-02 00:00:00 -0.4368855305400459
2021-02-03 00:00:00 -0.6135683390488923
2021-02-04 00:00:00 -0.7757942785174291
2021-02-05 00:00:00 -0.9177040258548256
2021-02-06 00:00:00 -1.0340083318420783
2021-02-07 00:00:00 -1.1204908143097292
2021-02-08 00:00:00 -1.1744351937165156
2021-02-09 00:00:00 -1.1948938876715327
2021-02-10 00:00:00 -1.1827399614722631
2021-02-11 00:00:00 -1.140494580853247
2021-02-12 00:00:00 -1.0719696672227224
2021-02-13 00:00:00 -0.9818037732776487
2021-02-14 00:00:00 -0.8749793418345923
2021-02-15 00:00:00 -0.7563965596976655
2021-02-16 00:00:00 -0.6305506630304736
2021-02-17 00:00:00 -0.5013285505901539
2021-02-18 00:00:00 -0.37191597325737136
2021-02-19 00:00:00 -0.24479140317840062
2021-02-20 00:00:00 -0.12177911524216488
2021-02-21 00:00:00 -0.00413500087411478      Print this value (last value)
2021-05-30 00:00:00 -0.004547066831108078     Condition True Print this value (first value)   
2021-05-31 00:00:00 -0.07962163144451284
2021-06-01 00:00:00 -0.15215400336574358
2021-06-02 00:00:00 -0.22137358654875092
2021-06-03 00:00:00 -0.28645563729756063
2021-06-04 00:00:00 -0.3465404682847117
2021-06-05 00:00:00 -0.4007583422689736
2021-06-06 00:00:00 -0.44825937136678057
2021-06-07 00:00:00 -0.48824672785691836
2021-06-08 00:00:00 -0.5200109481659706
2021-06-09 00:00:00 -0.5429628624480792
2021-06-10 00:00:00 -0.5566627097783102
2021-06-11 00:00:00 -0.5608427157269316
2021-06-12 00:00:00 -0.5554211491254732
2021-06-13 00:00:00 -0.5405060450116398
2021-06-14 00:00:00 -0.5163887400431304
2021-06-15 00:00:00 -0.4835277090835391
2021-06-16 00:00:00 -0.44252455305021243
2021-06-17 00:00:00 -0.394094049872733
2021-06-18 00:00:00 -0.3390307834305357
2021-06-19 00:00:00 -0.2781753626714067
2021-06-20 00:00:00 -0.21238292731356637
2021-06-21 00:00:00 -0.1424958524480665
2021-06-22 00:00:00 -0.06932129656902836          Print this value (last value)
2021-09-28 00:00:00 -0.08992145079448069          Condition True Print this value (first value)
2021-09-29 00:00:00 -0.2095165387227292
2021-09-30 00:00:00 -0.33367472841201334
2021-10-01 00:00:00 -0.460779986485703       
2021-10-02 00:00:00 -0.5885901793012869
2021-10-03 00:00:00 -0.7141710520264541
2021-10-04 00:00:00 -0.8338797124202194
2021-10-05 00:00:00 -0.9434261210721775
2021-10-06 00:00:00 -1.038038248150094
2021-10-07 00:00:00 -1.112746224561704
2021-10-08 00:00:00 -1.162778289034751
2021-10-09 00:00:00 -1.1840315451748653
2021-10-10 00:00:00 -1.1735509206575194
2021-10-11 00:00:00 -1.1299295237178415
2021-10-12 00:00:00 -1.0535472796478889
2021-10-13 00:00:00 -0.9465923196535948
2021-10-14 00:00:00 -0.812857555718251
2021-10-15 00:00:00 -0.6573542581978807
2021-10-16 00:00:00 -0.48582085619604937
2021-10-17 00:00:00 -0.30421497977143097
2021-10-18 00:00:00 -0.11826392085264098      Print this value (last value)

我不知道如何打印这些值,请帮助我,我被困在这里,我是绝对的初学者,所以请帮助我解决这个问题......非常感谢您提前!

【问题讨论】:

    标签: python date for-loop if-statement conditional-statements


    【解决方案1】:

    一个选项是将每个匹配项 psh 到一个列表中,然后只打印列表中的第一个和最后一个值:

    results = []
    for i in range(delta.days * 1 + 1):
        x = d1 + td(days=i)
        jd = Time(x).jd
        eph = swe.calc_ut(jd, 2, swe.FLG_SPEED+swe.FLG_JPLEPH)[0][3]
        if eph < 0:
            results.append((x, eph))
    
    print(results[0], results[-1])
    

    【讨论】:

    • 不工作,非常感谢
    • 你能解释一下它是怎么不工作的吗?
    • 先生,请阅读我的帖子并输出我真正想要打印的内容,您的代码只打印第一个和最后一个值,但我想在条件变为真时打印打印第一个和最后一个值,请参阅输出......跨度>
    • 此代码仅在if eph &lt; 0 时保存结果。如果这不算作“条件变为真”,请解释您在说什么条件?
    【解决方案2】:

    match 类似的解决方案,但仅保存第一个和最后一个值:

        rFirst = None
        rLast = None
        
        for i in range(delta.days * 1 + 1):
            x = d1 + td(days=i)
            jd = Time(x).jd
            eph = swe.calc_ut(jd, 2, swe.FLG_SPEED+swe.FLG_JPLEPH)[0][3]
            if eph < 0:
               if rFirst is None:
                   rFist = (x, eph)
               else:
                   rLast = (x, eph)
    
        print(rFirst, rLast)
    

    【讨论】:

    • 不工作,非常感谢
    • 代码现在应该可以工作了(即使我不能用你的 sn-ps 测试它)。如果你的意思是“不工作”结果是错误的,那么请澄清你的期望。
    【解决方案3】:

    您的示例代码中有太多依赖项,我无法提供经过测试的解决方案(即不是最少的代码),但作为一般方法,您可以跟踪先前的值并在满足条件时打印当前值和先前值(除了系统地打印第一个和最后一个):

    previous = None
    for i in range(delta.days * 1 + 1):
        x = d1 + td(days=i)
        jd = Time(x).jd
        eph = swe.calc_ut(jd, 2, swe.FLG_SPEED+swe.FLG_JPLEPH)[0][3]
        if eph < 0 or previous is None:
            if previous: print(*previous)
            print(x, eph)
        previous = (x, eph)
    print(*previous)
    

    【讨论】:

      最近更新 更多