Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate over dictionary of objects

I have a dictionary of objects which contains the "Names/Ranges" within a spreadsheet. As I process the spreadsheet I need to update the value associated with a range.

The class to hold this info looks like this:

class varName:
    name = None
    refersTo = None
    refersToR1C1 = None
    value = None
    def __init__(self, name, refersTo, refersToR1C1, value):
        self.name = name
        self.refersTo = refersTo
        self.refersToR1C1 = refersToR1C1
        self.value = value

I create the dictionary as follows:

staticNames = {}
wbNames = wb.Names
for name in wbNames:
    (nSheet, nAddr) = name.RefersTo.split("!") 
    print "Name:  %s    Refers to:  %s    Refers to R1C1:  %s       Value:  %s  " % (name.Name, name.RefersTo, name.RefersToR1C1, wSheets(nSheet.replace('=', '')).Range(nAddr).value) 
    staticNames[name.Name] = varName(name.Name, name.RefersTo, name.RefersToR1C1, wSheets(nSheet.replace('=', '') ).Range(nAddr).value)

It seems to work fine. I can see the dictionary and contained objects in debug. When I go back to update the objects within the dictionary based on processing the spreadsheet, I get lost. I call this function:

def updateStaticNames(ws, r, c, val_in, staticNames):
    for sName in staticNames:
        if sName.refersToR1C1() == "=" + ws.Name + "!R" + str(r) + "C" + str(c):
            sName.value = val_in          
    return None      

staticNames refers to the dictionary containing the Name/Range objects. I am expecting sName to contain an object of type varName. But alas it contains a string. What am I doing wrong?

like image 415
KBD Avatar asked Jun 23 '15 15:06

KBD


People also ask

How do I iterate through a dictionary item?

In order to iterate over the values of the dictionary, you simply need to call values() method that returns a new view containing dictionary's values.

Can you iterate over a dictionary?

Dictionaries are iterable objects, which means you can iterate through them like any other object.

How do you iterate over an array of objects in Python?

Iterate through array itemslength tells our for loop to continue as long as i is less than the array's length (in this case, 7). i++ is equivalent to i+1 and means we're incrementing through our array by one each time. We could also use i+2 to proceed with every other item in the array, for example.

How do you iterate through a dictionary using a for loop?

In Python, to iterate the dictionary ( dict ) with a for loop, use keys() , values() , items() methods. You can also get a list of all keys and values in the dictionary with those methods and list() . Use the following dictionary as an example. You can iterate keys by using the dictionary object directly in a for loop.


1 Answers

for foo in some_dict iterates through the keys of a dictionary, not its values.

d = {'a': 1, 'b': 2, 'c': 3}
for dd in d:
    print(dd)
# gives a; b; c

You probably want to do for foo in some_dict.values()

for dd in d.values():
    print(dd)
# gives 1; 2; 3
like image 91
Adam Smith Avatar answered Oct 15 '22 11:10

Adam Smith