Karen Jiang
Karen Jiang

Reputation: 175

python dictionary look for value returns wrong result

#yearly_weather
import os
from datetime import datetime, date
import datetime
test={datetime.date(1977, 6, 5): [81, 55, 0.0, False],
 datetime.date(2007, 11, 18): [45, 41, 0.02, True],
 datetime.date(2017, 11, 3): [43, 33, 0.3, True],
 datetime.date(2007, 2, 24): [44, 36, 0.36, True],
 datetime.date(2015, 2, 18): [54, 40, 0.0, False],
 datetime.date(2008, 11, 18): [51, 43, 0.01, True],
 datetime.date(1965, 12, 29): [43, 33, 0.27, True]}


#yearly_weather={year:[AVG_TMAX, AVG_TMIN, TOTAL_PRCP, TOTAL_RAINY_DAYS, TOTAL_DAYS]}  
# datetime.date(1965, 12, 29): [43, 33, 0.27, True]}
#{2017: [45.666666666666664, 34.333333333333336, 2.28, 9, 15]}
temp={}
yearly_weather={}
for k,v in test.items():
    year=k.year
    value=temp.get(k.year,(0,0,0,0,0,0))
    sumtmax=v[0]+value[0]
    counttmax=value[1]+1
    sumtmin=v[1]+value[2]
    counttmin=value[3]+1
    sumprcp=v[2]+value[4]
    sumrainy=v[3]+value[5]
    sumdays=counttmax
    temp[k.year]=(sumtmax,counttmax,sumtmin,counttmin,sumprcp,sumrainy,sumdays)
    #print(temp)
for k,v in temp.items():
    yearly_weather[k]=(v[0]/v[1],v[2]/v[3],v[4],v[5],v[6])

print(yearly_weather)

def year_info(year, yearly_weather):
    for k,v in yearly_weather.items():
        if year==k:
            return "{:<8.0f}| {:<18.2f}| {:>12.2f} | {:>10.2f} | {:>10.0f}| {:>10.0f}" .format(k,v[0],v[1],v[2],v[3],v[4])

        else:
            return "{:^8s}| {:^18s}| {:^12s} | {:^10s} | {:^10s} | {:^10s}".format("N/A","N/A","N/A","N/A","N/A","N/A")


year="1965"
year=int(year)
year_info(year,yearly_weather)

  N/A   |        N/A        |     N/A      |    N/A     |    N/A     |    N/A  

when I try to look for a specific year's info, its supposed to return weather information, why it turns N/A?

ignore: repeat question for word requirements ignore: repeat question for word requirements

Upvotes: 0

Views: 446

Answers (2)

jpp
jpp

Reputation: 164673

There's no need to iterate your dictionary. Just access the appropriate dictionary item directly. If you need to deal with KeyError, you can use a try / except clause:

def year_info(year, yearly_weather):
    try:
        k = year
        v = yearly_weather[k]
        return "{:<8.0f}| {:<18.2f}| {:>12.2f} | {:>10.2f} | {:>10.0f}| {:>10.0f}" .format(k, *v)
    except KeyError:
        return "{:^8s}| {:^18s}| {:^12s} | {:^10s} | {:^10s} | {:^10s}".format(*['N/A']*6)

Result

res = year_info(1965, yearly_weather)

'1965    | 43.00             |        33.00 |       0.27 |          1|          1'

Upvotes: 3

damisan
damisan

Reputation: 1047

First year is 1977, it is different from 1965, so the function returns "N/A". It doesn't check the next item in the set.

Try this code instead:

def year_info(year, yearly_weather):
    for k,v in yearly_weather.items():
        if year==k:
            return "{:<8.0f}| {:<18.2f}| {:>12.2f} | {:>10.2f} | {:>10.0f}| {:>10.0f}" .format(k,v[0],v[1],v[2],v[3],v[4])

    return "{:^8s}| {:^18s}| {:^12s} | {:^10s} | {:^10s} | {:^10s}".format("N/A","N/A","N/A","N/A","N/A","N/A")

Upvotes: 1

Related Questions