简体   繁体   中英

Overwriting all values while iterating a for loop- Python

I'm new to python and have been working to parse an excel spreadsheet. I'm trying to determine the accumulated value associated with a particular zoning over a series of dates.

I have a feeling that I'm not understanding the logic flow correctly, because no matter how I write it, I keep ending up with a dictionary of values that are all the same. At this point, I don't have an intuition for why it's wrong, so instead of writing around it, I'd like to face it head on.

The hoursAllocationDict looks like:

5-21-16
    Zoning1: 0
    Zoning2: 0
    Zoning3: 0
5-22-16
    Zoning1: 0
etc...

My rawData looks like a list of lists:

[0] NAME, data, data, data, DATE, data, HOURS, data, ZONING, data, data, data, etc. 
[1] NAME, data, data, data, DATE, data, HOURS, data, ZONING, data, data, data, etc. 
[2] NAME, data, data, data, DATE, data, HOURS, data, ZONING, data, data, data, etc. 

The code block I'm running for this particular task looks like:

#Iterate over all dates - date is a tuple with 0 index being the date and 1 being a dict of zonings
for date in hoursAllocationDict.iteritems():

    #Iterate over each row
    for row in rawData:

        #If cell is not empty or blank AND if date cell equals iterator date
        if rawData[row][23] and rawData[row][9] == date[0]:

            #Use re.search to match possible zoning in zoning column (found in string of otherwise irrelevant data)

            if findZoningInCell(rawData[row][23], zoningsDict):


                #Store whatever subjoining we find
                subZoning = findZoningInCell(rawData[row][23], zoningsDict)

                #rawData[row][18] references a value of hours related to zoning

                #Accumulate x.x hrs in hoursAllocationDict -> date -> subjoining

                hoursAllocationDict[rawData[row][9]][subZoning] += rawData[row][18]

The final state of hoursAllocationDict looks like:

'10-29-15' : 'Zoning1': 52.0, 'Zoning2': 100.08333333333333, 'Zoning3': 128.0, 'Zoning4': 594.0, etc...
'10-30-15' : 'Zoning1': 52.0, 'Zoning2': 100.08333333333333, 'Zoning3': 128.0, 'Zoning4': 594.0, etc...
'10-31-15' : 'Zoning1': 52.0, 'Zoning2': 100.08333333333333, 'Zoning3': 128.0, 'Zoning4': 594.0, etc...
....
....

So I'm somehow updating all values of all keys of the dictionary every iteration, but I just can't see how. I've rewritten it a couple of times to now avail.

I figured out the answer.

The code immediately preceding this segment is:

#Set structure of hoursAllocationDict
#Date:
#        Zoning1: 0
#        Zoning2: 0

for date in uniqueDateList:
    hoursAllocationDict[date] = zoningsDict

Looking at how Python handles assignment (from 8.17 "copy"):

Assignment statements in Python do not copy objects, they create bindings between a target and an object. For collections that are mutable or contain mutable items, a copy is sometimes needed so one can change one copy without changing the other.

Changing the above code to the following resolved the issue:

from copy import copy

....

 for date in uniqueDateList:
    hoursAllocationDict[date] = copy(zoningsDict)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM