简体   繁体   English

如何使用循环访问 python 字典中的某些值?

[英]How do I access certain values in a python dictionary using a loop?

I have a python dictionary that I only need to access certain values from.我有一个 python 字典,我只需要从中访问某些值。 I need to access the Date, HighTemp, LowTemp, Windspeed and Rainfall values.我需要访问 Date、HighTemp、LowTemp、Windspeed 和 Rainfall 值。

I have tried using this for loop:我试过使用这个 for 循环:

for Forecast in weather_data['value']['Forecasts'][0]:
    print(Forecast)

I am stuck and I'm not sure how to get those values.我被卡住了,我不确定如何获得这些值。 Any help will be greatly appreciated!任何帮助将不胜感激!

This is the dictionary:这是字典:

{'value': {'City': '77107', 'CityName': 'Cape Town', 'Forecasts': [{'DaySequence': 1, 'Segment': None, 'DaySegment': None, 'DayOfWeek': 6, 'WeekDay': 'Friday', 'ShortWeekDay': 'Fri', 'Date': '/Date(1548367200000)/', 'FormattedDate': 'Fri, Jan 25', 'DayLight': 'D', 'SkyDescriptor': '1', 'Sky': 'Sunny', 'PrecipitationDescriptor': '', 'Precipitation': '', 'TemperatureDescriptor': '8', 'TemperatureDescription': 'Mild', 'HighTemp': '22', 'LowTemp': '17', 'UV': 'High', 'AirDescription': '20', 'Air': 'Beautiful', 'Description': 'Sunny. Mild.', 'Temperature': None, 'WindSpeed': '54', 'Beaufort': None, 'BeaufortDescriptor': None, 'WindDirection': None, 'WindDirectionAbreviated': None, 'WindDirectionDescription': None, 'DewPoint': None, 'Humidity': None, 'Comfort': None, 'Visibility': None, 'Rainfall': None, 'Snowfall': None, 'PrecipitationProbability': None, 'Icon': '1', 'IconName': 'sunny', 'Cached': False}, {'DaySequence': 2, 'Segment': None, 'DaySegment': None, 'DayOfWeek': 7, 'WeekDay': 'Saturday', 'ShortWeekDay': 'Sat', 'Date': '/Date(1548453600000)/', 'FormattedDate': 'Sat, Jan 26', 'DayLight': 'D', 'SkyDescriptor': '1', 'Sky': 'Sunny', 'PrecipitationDescriptor': '', 'Precipitation': '', 'TemperatureDescriptor': '10', 'TemperatureDescription': 'Warm', 'HighTemp': '28', 'LowTemp': '18', 'UV': 'High', 'AirDescription': '22', 'Air': 'Nice', 'Description': 'Sunny. Warm.', 'Temperature': None, 'WindSpeed': '21', 'Beaufort': None, 'BeaufortDescriptor': None, 'WindDirection': None, 'WindDirectionAbreviated': None, 'WindDirectionDescription': None, 'DewPoint': None, 'Humidity': None, 'Comfort': None, 'Visibility': None, 'Rainfall': None, 'Snowfall': None, 'PrecipitationProbability': None, 'Icon': '1', 'IconName': 'sunny', 'Cached': False}, {'DaySequence': 3, 'Segment': None, 'DaySegment': None, 'DayOfWeek': 1, 'WeekDay': 'Sunday', 'ShortWeekDay': 'Sun', 'Date': '/Date(1548540000000)/', 'FormattedDate': 'Sun, Jan 27', 'DayLight': 'D', 'SkyDescriptor': '1', 'Sky': 'Sunny', 'PrecipitationDescriptor': '', 'Precipitation': '', 'TemperatureDescriptor': '8', 'TemperatureDescription': 'Mild', 'HighTemp': '24', 'LowTemp': '20', 'UV': 'High', 'AirDescription': '29', 'Air': 'Comfortable', 'Description': 'Sunny. Mild.', 'Temperature': None, 'WindSpeed': '51', 'Beaufort': None, 'BeaufortDescriptor': None, 'WindDirection': None, 'WindDirectionAbreviated': None, 'WindDirectionDescription': None, 'DewPoint': None, 'Humidity': None, 'Comfort': None, 'Visibility': None, 'Rainfall': None, 'Snowfall': None, 'PrecipitationProbability': None, 'Icon': '1', 'IconName': 'sunny', 'Cached': False}, {'DaySequence': 4, 'Segment': None, 'DaySegment': None, 'DayOfWeek': 2, 'WeekDay': 'Monday', 'ShortWeekDay': 'Mon', 'Date': '/Date(1548626400000)/', 'FormattedDate': 'Mon, Jan 28', 'DayLight': 'D', 'SkyDescriptor': '1', 'Sky': 'Sunny', 'PrecipitationDescriptor': '', 'Precipitation': '', 'TemperatureDescriptor': '8', 'TemperatureDescription': 'Mild', 'HighTemp': '26', 'LowTemp': '19', 'UV': 'High', 'AirDescription': '20', 'Air': 'Beautiful', 'Description': 'Sunny. Mild.', 'Temperature': None, 'WindSpeed': '32', 'Beaufort': None, 'BeaufortDescriptor': None, 'WindDirection': None, 'WindDirectionAbreviated': None, 'WindDirectionDescription': None, 'DewPoint': None, 'Humidity': None, 'Comfort': None, 'Visibility': None, 'Rainfall': None, 'Snowfall': None, 'PrecipitationProbability': None, 'Icon': '1', 'IconName': 'sunny', 'Cached': False}, {'DaySequence': 5, 'Segment': None, 'DaySegment': None, 'DayOfWeek': 3, 'WeekDay': 'Tuesday', 'ShortWeekDay': 'Tue', 'Date': '/Date(1548712800000)/', 'FormattedDate': 'Tue, Jan 29', 'DayLight': 'D', 'SkyDescriptor': '3', 'Sky': 'Mostly sunny', 'PrecipitationDescriptor': '4', 'Precipitation': 'a few showers', 'TemperatureDescriptor': '8', 'TemperatureDescription': 'Mild', 'HighTemp': '24', 'LowTemp': '19', 'UV': 'High', 'AirDescription': '20', 'Air': 'Beautiful', 'Description': 'a few showers. Mostly sunny. Mild.', 'Temperature': None, 'WindSpeed': '35', 'Beaufort': None, 'BeaufortDescriptor': None, 'WindDirection': None, 'WindDirectionAbreviated': None, 'WindDirectionDescription': None, 'DewPoint': None, 'Humidity': None, 'Comfort': None, 'Visibility': None, 'Rainfall': None, 'Snowfall': None, 'PrecipitationProbability': None, 'Icon': '33', 'IconName': 'showers', 'Cached': False}, {'DaySequence': 6, 'Segment': None, 'DaySegment': None, 'DayOfWeek': 4, 'WeekDay': 'Wednesday', 'ShortWeekDay': 'Wed', 'Date': '/Date(1548799200000)/', 'FormattedDate': 'Wed, Jan 30', 'DayLight': 'D', 'SkyDescriptor': '8', 'Sky': 'More sun than clouds', 'PrecipitationDescriptor': '51', 'Precipitation': 'Showers early', 'TemperatureDescriptor': '6', 'TemperatureDescription': 'Cool', 'HighTemp': '22', 'LowTemp': '19', 'UV': 'High', 'AirDescription': '*', 'Air': '', 'Description': 'Showers early. More sun than clouds. Cool.', 'Temperature': None, 'WindSpeed': '27', 'Beaufort': None, 'BeaufortDescriptor': None, 'WindDirection': None, 'WindDirectionAbreviated': None, 'WindDirectionDescription': None, 'DewPoint': None, 'Humidity': None, 'Comfort': None, 'Visibility': None, 'Rainfall': None, 'Snowfall': None, 'PrecipitationProbability': None, 'Icon': '33', 'IconName': 'showers', 'Cached': False}, {'DaySequence': 7, 'Segment': None, 'DaySegment': None, 'DayOfWeek': 5, 'WeekDay': 'Thursday', 'ShortWeekDay': 'Thu', 'Date': '/Date(1548885600000)/', 'FormattedDate': 'Thu, Jan 31', 'DayLight': 'D', 'SkyDescriptor': '32', 'Sky': 'Morning clouds', 'PrecipitationDescriptor': '', 'Precipitation': '', 'TemperatureDescriptor': '6', 'TemperatureDescription': 'Cool', 'HighTemp': '20', 'LowTemp': '17', 'UV': 'High', 'AirDescription': '29', 'Air': 'Comfortable', 'Description': 'Morning clouds. Cool.', 'Temperature': None, 'WindSpeed': '55', 'Beaufort': None, 'BeaufortDescriptor': None, 'WindDirection': None, 'WindDirectionAbreviated': None, 'WindDirectionDescription': None, 'DewPoint': None, 'Humidity': None, 'Comfort': None, 'Visibility': None, 'Rainfall': None, 'Snowfall': None, 'PrecipitationProbability': None, 'Icon': '4', 'IconName': 'partly_cloudy', 'Cached': False}], 'Cached': False}}

Just iterate over the wanted keys:只需迭代所需的键:

for forecast in ['Date', 'HighTemp', 'LowTemp', 'Rainfall', 'Windspeed']:
  print weather_data['value']['Forecast'][forecast]

weather_data['value']['Forecasts'] is a list of dicts of the same keys, so iterate through that list: weather_data['value']['Forecasts']是相同键的字典列表,因此遍历该列表:

for forecast in weather_data['value']['Forecasts']:
     print(forecast['Date'], forecast['HighTemp'], forecast['LowTemp'], 
           forecast['WindSpeed'], forecast['Rainfall'])

/Date(1548367200000)/ 22 17 54 None
/Date(1548453600000)/ 28 18 21 None
/Date(1548540000000)/ 24 20 51 None
/Date(1548626400000)/ 26 19 32 None
/Date(1548712800000)/ 24 19 35 None
/Date(1548799200000)/ 22 19 27 None
/Date(1548885600000)/ 20 17 55 None

This is a good case to use a pandas dataframe:这是使用 Pandas 数据框的一个很好的例子:

df = pd.DataFrame(d['value']['Forecasts'])
df.index = pd.to_datetime(df['Date'].str[6:19], unit='ms')
df[['HighTemp','LowTemp','WindSpeed','Rainfall']]

                    HighTemp LowTemp WindSpeed Rainfall
Date
2019-01-24 22:00:00       22      17        54     None
2019-01-25 22:00:00       28      18        21     None
2019-01-26 22:00:00       24      20        51     None
2019-01-27 22:00:00       26      19        32     None
2019-01-28 22:00:00       24      19        35     None
2019-01-29 22:00:00       22      19        27     None
2019-01-30 22:00:00       20      17        55     None

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何访问 python 中字典中的某个列表条目? - How do I access a certain list entry in a dictionary in python? 如何使用for循环和带用户输入的if语句在python字典中的键/值之间循环? - How do I cycle through keys/values in a python dictionary using a for loop and if statement with user input? 如何从 Python 的 for 循环中的列表中获取某些值? - How do I get certain values from a list in a for loop in Python? Python - 如何遍历字典来检查特定文本的值? - Python - How do you loop through a dictionary to check the values for certain text? 如何访问Python创建的字典 - How do I access a Python created dictionary 如何在python中创建for循环以使用单词的字符访问字典的键值 - How to create a for loop in python to access the key values of a dictionary using the characters of a word 如果值是日期,如何遍历字典中的所有值 - How do I loop through all values in a dictionary if the values are dates Python:如何格式化循环以在字典列表中提取特定值? - Python: How do I format a loop to pull specific values in a dictionary list? 如何遍历字典并检查Python中变量传递的值 - How do I loop over dictionary and check for values passed by a variable in Python Python - 如何更新具有一个键和两个值的循环的字典? - Python - How do I update a dictionary with a loop that has one key and two values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM