Python在退出循环scop时发布属性更改的值

2024-10-04 03:26:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我在编写一个python类时遇到了问题,特别的问题是parseData()函数

在函数的末尾,我已经指出了问题所在。问题是,当我完成代码的内部循环,指向parseData()函数的底部时,self.listOfParsedWeatherData的值是正确的,但是一旦作用域从第二个循环中掉出来,self.listOfParsedWeatherData中每个元素的值都是上一个元素的副本

基本上,当我在第二个循环的范围内检查列表的值时,它类似于[0,1,2,3,4,5],但是当我在第二个循环之外检查同一个列表的值时,它显示为 [5, 5, 5, 5, 5, 5].

我确信这与范围或者深浅副本有关,但是我对python语言还是相当陌生的,我不确定其中的一些复杂之处。尽管我怀疑这个问题是不是很复杂

任何帮助都将不胜感激该类如下所示,我插入的对象是我编写的一个简单的自定义类

# imports
import pywapi                            # used to grab weather data from NOAA in a JSON format
import copy
from WeatherData import WeatherData     # import custom weatherdata object

import pprint 
pp = pprint.PrettyPrinter(indent=4)     # set up pretty printer object

##
## Class used for grabing and parsing weather data from 
## NOAA
##
class NOAAParser:
# initliazer for the NOAAParser class
def __init__(self):
    # stores the list of json city data
    self.listOfCityData = []

    # A list of weatherData
    self.listOfParsedWeatherData = []

##
## function to grab the weather and returns a JSON object of the weather data
##
## Parameters: self, dictionary of cities and their codes
##
def retrieveNOAAWeatherJson(self, cityCodes):
    # loop through city codes and append them to a list of 
    cityData = []

    # weather objects returned from pywapi
    for key, value in cityCodes.iteritems():
        cityData.append(pywapi.get_weather_from_noaa(value))

    # set the list of city json data 
    self.listOfCityData = cityData

##
## This function parses the listOfCityData and returns a list 
## of weatherData objects which can be displayed to the user
##
##
def parseData(self):
    # check if the listOfCities has been populated
    if not self.listOfCityData:
        return False

    else:
        # create a new object of weather data
        newWeatherData = WeatherData()

        # loop over list and parse the data
        for index in range(len(self.listOfCityData)):
            # loop over the dictionary values in the list
            for key, value in self.listOfCityData[index].iteritems():
                # grab weather Description key: "weather" (String)
                if key == "weather":
                    # print value
                    newWeatherData.weatherCondition = value

                # grab the location key: "location" (String)
                if key == "location":
                    # print value
                    newWeatherData.location = value

                # grab temp key: "temperature_string" (String)
                if key == "temperature_string":
                    # print value
                    newWeatherData.currentTemp = value

                # grab Humidity key: "relative_humidity" (Int)
                if key == "relative_humidity":
                    # print value
                    newWeatherData.humidity = value

                # grab wind direction key: "wind_dir" (String)
                if key == "wind_dir":
                    # print value
                    newWeatherData.windDirection = value

                # grab last updated time key: "observation_time" (String)
                if key == "observation_time":
                    # print value
                    newWeatherData.lastUpdated = value

            # append the new weather data object to the list
            self.listOfParsedWeatherData.append(newWeatherData)

            ## VALUE OF self.listOrParsedWeatherData is correct

    ## VALUE OF self.listOfParsedWeatherData is only the value of the last element 
    ## of the previous self.listOfParsedWeatherData (incorrect)

    # return success from the function
    return True

Tags: andofthekeyfromselfdataif
1条回答
网友
1楼 · 发布于 2024-10-04 03:26:11

之所以会发生这种情况,是因为您只创建了WeatherData类的一个实例,所以您只需不断地更新同一个实例

您只需要将行newWeatherData = WeatherData()移动到第一个for循环的内部

相关问题 更多 >