Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have the following dictionary:

zimmerliste = {'bad_eg' : ((5,2),(5,2),(5,2),(40,.05)), 'gang_eg' : ((7,2,),(40,1.5))}

The key is a room in my house. The dictionary contains a tuple of a tuple. Each inside tuple contains a wattage number (of a lamp) and how long it will burn per day.

I now want to have the wattage sum per room. This code of my room-object doesn't work:

def watt_pro_zimmer(self):             .
    iterator = 0
    for x in zimmerliste:
        for tup in zimmerliste[x]:
            iterator += tup[0]
    return iterator

Where's my error and is there an easier way?

share|improve this question

closed as off-topic by Avinash Raj, i08in, Mitch May 9 at 9:00

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This is not about Ubuntu. Questions about other Linux distributions can be asked on Unix & Linux, those about Windows on Super User, those about Apple products on Ask Different and generic programming questions on Stack Overflow." – Avinash Raj, i08in, Mitch
If this question can be reworded to fit the rules in the help center, please edit the question.

2 Answers 2

I think you want:

def watt_pro_zimmer(self):
    iterator = 0
    for x in zimmerliste.values():
        for tup in x:
                iterator += tup[0]
    return iterator

zimmerliste is a key-value pair, you forgot to take only the value part.

share|improve this answer
    
Should be the same as i iterate over values in my second loop. I still got File "<stdin>", line 4 return iterator ^ SyntaxError: invalid syntax with your code. –  empedokles May 9 at 8:43
    
I get 102, which is what you want, isn't it? –  Jos May 9 at 8:45
    
I get >>> iterator = 0 >>> for x in zimmerliste.values(): ... for tup in x: ... iterator += tup[0] ... return iterator File "<stdin>", line 4 return iterator ^ SyntaxError: invalid syntax ? –  empedokles May 9 at 8:57
    
Forgot the : at the end of the line starting with def. Will edit. –  Jos May 9 at 9:01
1  
Then don't use a return statement, use a print statement. –  Jos May 9 at 9:06

Try this way, using zip() and sum():

zimmerliste = {'bad_eg' : ((5,2),(5,2),(5,2),(40,.05)), 'gang_eg' : ((7,2,),(40,1.5))}

def watt_pro_zimmer():
  for k, v in zimmerliste.iteritems():
    return [sum(x) for x in zip(*v)]

This returns the sum of watts and how long it will burn per room in a tuple:

[55, 6.05]
[47, 3.5]
share|improve this answer
    
I don't understand what zip(*v) does. –  empedokles May 9 at 9:01
    
zip() is a built-in function: "This function returns a list of tuples, in conjunction with the * operator can be used to unzip a list." –  girardengo May 9 at 9:17
    
@giardengo Over my head I guess. How would this look as list comprehension: 'code'iterator = 0 for tup in zimmerliste[self.name]: iterator += tup[0] return iterator –  empedokles May 9 at 9:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.