Tell me more ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

I am trying to sum the values in 6th key of a dictionary, the following is my script

import re import string import sys import fileinput import os import random import glob import getopt import collections

def portfolio(): # Listing all values

ValueList = [['20120101', '@@a', 'FRR', 'MSFT', 'Trade','100','20'],
             ['20120102', '@@a', 'FRR', 'AAPLR', 'Trade','110','30'],
             ['20120103', '@@a', 'FRR', 'INTLR', 'Quote','120','40'],
             ['20120104', '@@a', 'FRR', 'MSFT'R, 'Quote','130','50']]

# Adding Dictionary

dict = {}

for stkvalue in ValueList:
    val = string.split(stkvalue[0])
    noQts = val[-1]
    dict[noQts] = stkvalue

    # Sort the dictionary keys

    srtKeys =  dict.keys()
    srtKeys.sort()

    #print "%-20s  Number" % 'Name'
    #print "="*34

for noQts in srtKeys:
    name = dict[noQts][0]
    Number =  dict[noQts][1]
    num2 = dict[noQts][2]
    num3 = dict [noQts][3]
    num4 = dict [noQts][4]
    num5 = dict [noQts][5]
    num6 = dict [noQts][6]

    #print "%-20s  %s" %(name,Number,num2)
    #print name,Number,num2,num3,num4,num5
    print sum(num6)

portfolio()

The Error message I get is "TypeError: unsupported operand type(s) for +: 'int' and 'str'"

  • How do I resolve this ?
  • Using Sum(num6) doesnt work at all, is there any alternate solution ?
share|improve this question
Is this a testing question? – user246 Jul 9 '12 at 18:07
2  
Sid, I attempted to move this question to SO, on which being it has nothing to do with testing it clearly belongs. In doing so, I found that you're blocked from asking questions there. Please make your questions HERE related to testing as defined by the FAQ. Posting to any old SE site to avoid a ban on SO is not an acceptable course of action. – corsiKa Jul 9 '12 at 19:31

closed as off topic by corsiKa Jul 9 '12 at 19:08

Questions on Software Quality Assurance & Testing Stack Exchange are expected to relate to software quality assurance or testing within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

1 Answer

res = sum([int(x[6]) for x in ValueList])

if the values are integers.

share|improve this answer
My sincere apologies and i will make sure that this doesn't happen next time.I had no clue that I was banned in SE, my bad. – Siddharth Ravindran Jul 10 '12 at 7:33

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