【问题标题】:how to add user input to a dictionary如何将用户输入添加到字典
【发布时间】:2019-07-31 15:27:11
【问题描述】:

我正在尝试从天气网站的 api 构建图表。我希望用户输入我想知道当前温度的 3 个城市,然后从 api 和用户输入 {name of the city - user input: the currently temp from the api} 制作字典,然后将字典制作成图表 {name of the city: the currently temp from the api}

我使用的api是drksky api 我主要使用这个:

import matplotlib.pyplot as plt for the graph
import forecastio
import matplotlib.pyplot as plt



def read_key_from_file():
    hFile = open(file_name,'r')
    for line in hFile:
        Mykey = line
    hFile.close()
    Mykey = line
    return  Mykey

def get_geo_location(location):
    location = Nominatim().geocode(location)
    lat = location.latitude
    lon = location.longitude
    return location, lat, lon


def comperison_graph(Mykey): 

    if daily_temp_dict == {}:
        a = input("Enter first city name in lower case letter: ")
        daily_temp_dict[a] = ""
        b = input("Enter second city name in lower case letter: ")
        daily_temp_dict[b] = ""
        c = input("Enter third city name in lower case letter: ")
        daily_temp_dict[c] = ""
        for city in daily_temp_dict.keys():
                location, lat, lon = get_geo_location(city)
                forecast = forecastio.load_forecast(Mykey, lat, lon)
                daily_temp_dict[city] = forecast.currently().temperature
                data =  daily_temp_dict
                names = list(data.keys())
                print(names)
                values = list(data.values())
                print(values)
                plt.bar(0,values[0],tick_label=names[0])
                plt.bar(1,values[1],tick_label=names[1])
                plt.bar(2,values[2],tick_label=names[2])
                plt.xticks(range(0,3),names)
                plt.savefig('fruit.png')
                plt.show()

问题是我得到的结果是这样的:

https://imgur.com/raVSVxH

我只需要三个城市的最后一个

很遗憾,我做不到

有人可以帮帮我吗?

【问题讨论】:

  • 很遗憾我做不到 - 为什么?有错误吗?输出不是你期望的那样吗?
  • 您好,请对您的问题进行一些编辑以增加可读性,您将获得更多关注和更多机会获得答案。
  • 它应该是字典中的图表(x 图:城市名称,y 图:来自 api 的当前温度)
  • 这条线是什么? end if
  • 我修复了代码请有人可以帮助我?

标签: python python-3.x dictionary


【解决方案1】:

字典不是这样工作的,字典不支持 append()

我认为你不知道字典是如何工作的,我建议你读一点

https://www.tutorialspoint.com/python3/python_dictionary

https://www.programiz.com/python-programming/dictionary

【讨论】:

  • 我对这个主题有所了解并编辑了我的问题,你现在能帮我吗?
【解决方案2】:

您应该使用空字符串"" 作为值创建一个新键:

if cities_for_txt_file == {}:
    a = input("Enter first city name in lower case letter: ")
    cities_for_txt_file[a] = ""
    b = input("Enter second city name in lower case letter: ")
    cities_for_txt_file[b] = ""
    c = input("Enter third city name in lower case letter: ")
    cities_for_txt_file[c] = ""
    for city in cities_for_txt_file:
            location, lat, lon = get_geo_location(city)
            forecast = forecastio.load_forecast(Mykey, lat, lon)

然后将forecast 赋值给城市:

            cities_for_txt_file[city] = forecast

注意:由于您没有提供完整代码,因此它很可能无法处理错误,例如您的get_geo_location() 中找不到城市。我建议您在代码中添加一些“try/catch”。

编辑:当您编辑问题时,这是我对您的新问题的回答。

你不应该在循环中做所有的绘图部分,而是在它之后。它应该是这样的:

    for city in daily_temp_dict.keys():
        location, lat, lon = get_geo_location(city)
        forecast = forecastio.load_forecast(Mykey, lat, lon)
        daily_temp_dict[city] = forecast.currently().temperature
    data =  daily_temp_dict
    names = list(data.keys())
    print(names)
    values = list(data.values())
    print(values)
    plt.bar(0,values[0],tick_label=names[0])
    plt.bar(1,values[1],tick_label=names[1])
    plt.bar(2,values[2],tick_label=names[2])
    plt.xticks(range(0,3),names)
    plt.savefig('fruit.png')
    plt.show()

【讨论】:

  • 它来自 api,你能从这里帮助我如何将它变成一个图 matplotlib.pyplot as plt
  • from geopy.geocoders import Nominatim get_geo_location(location): location = Nominatim().geocode(location) lat = location.latitude lon = location.longitude return location, lat, lon 来自
  • @kinglebron 我为你的新问题编辑了答案。
  • @kinglebron 如果解决了您的问题,请将其标记为答案
猜你喜欢
  • 2016-07-06
  • 2020-06-27
  • 1970-01-01
  • 1970-01-01
  • 2019-11-30
  • 1970-01-01
  • 1970-01-01
  • 2014-10-02
  • 1970-01-01
相关资源
最近更新 更多