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 ?