Home > Software design >  Is there's a way to print only first and last value in loop when given condition become true?
Is there's a way to print only first and last value in loop when given condition become true?

Time:12-05

Hello every one I am working on a project and I want to print only first and last value of given condition when condition become true ...

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)

Output ---

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)

I don't know how can I print this values please help me I'm stuck here and I'm absolute beginner so please help me in this problem ... Thank You Very much in advance !

CodePudding user response:

One option is to psh every match onto a list, but then only print the first and last values from the list:

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

CodePudding user response:

A similar solution as from match, but where only the first and last value is saved:

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)
  • Related