471,354 Members | 1,704 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 software developers and data experts.

Summing into a Dictionary

2
Hi,

Programming in perl and running through lines in a text file, when wanting to sum the area by a key (repeating) variable I am used to using the syntax:
$Hash{$var1}[0] += $area
And then using a foreach loop to get out the total area for each variable.
What is the translation into python for this?

Thanks
Jul 18 '07 #1
5 1517
ghostdog74
511 Expert 256MB
Hi,

Programming in perl and running through lines in a text file, when wanting to sum the area by a key (repeating) variable I am used to using the syntax:
$Hash{$var1}[0] += $area
And then using a foreach loop to get out the total area for each variable.
What is the translation into python for this?

Thanks
Expand|Select|Wrap|Line Numbers
  1. myDict[var1][0]+=area
  2.  
Jul 18 '07 #2
tbline
2
Expand|Select|Wrap|Line Numbers
  1. myDict[var1][0]+=area
  2.  
That is what I have been trying, however when I do, I get the following error:

myDict[au][0]+=area
KeyError: 5

This is the jist of my code so far:
Expand|Select|Wrap|Line Numbers
  1. myDict={}
  2.  
  3. for line in f:
  4.         vals = line.strip('\n').split('|')
  5.         stand_num = vals[0]
  6.         access    = vals[3]
  7.         au        = int(vals[7])
  8.         area      = float(vals[9])
  9.  
  10.  
  11.         if au != 99:
  12.                  myDict[au][0]+=area

What does KeyError:5 mean and how I can fix it?

Thanks
Jul 19 '07 #3
bvdet
2,851 Expert Mod 2GB
That is what I have been trying, however when I do, I get the following error:

myDict[au][0]+=area
KeyError: 5

This is the jist of my code so far:

myDict={}

for line in f:
vals = line.strip('\n').split('|')
stand_num = vals[0]
access = vals[3]
au = int(vals[7])
area = float(vals[9])


if au != 99:
myDict[au][0]+=area


What does KeyError:5 mean and how I can fix it?

Thanks
myDict does not contain the key 5, therefore the key error. Try this instead:
Expand|Select|Wrap|Line Numbers
  1. if myDict.has_key(au):
  2.     myDict[au][0] += area
  3. else:
  4.     myDict[au] = whatever
Jul 19 '07 #4
bartonc
6,596 Expert 4TB
That is what I have been trying, however when I do, I get the following error:

myDict[au][0]+=area
KeyError: 5

This is the jist of my code so far:
Expand|Select|Wrap|Line Numbers
  1. myDict={}
  2.  
  3. for line in f:
  4.         vals = line.strip('\n').split('|')
  5.         stand_num = vals[0]
  6.         access    = vals[3]
  7.         au        = int(vals[7])
  8.         area      = float(vals[9])
  9.  
  10.  
  11.         if au != 99:
  12.                  myDict[au][0]+=area

What does KeyError:5 mean and how I can fix it?

Thanks
See how much nicer it looks when CODE tags are used. To learn how to use them, look to the right. Third point down under REPLY GUIDELINES. Thanks for taking the time,
Moderator
Jul 19 '07 #5
elbin
27
myDict does not contain the key 5, therefore the key error. Try this instead:
Expand|Select|Wrap|Line Numbers
  1. if myDict.has_key(au):
  2.     myDict[au][0] += area
  3. else:
  4.     myDict[au] = whatever
And the 'whatever' should be a list with the area if you're going to use the [0] index:
Expand|Select|Wrap|Line Numbers
  1. if myDict.has_key(au):
  2.     myDict[au][0] += area
  3. else:
  4.     myDict[au] = [area]
Or simpler (no index if there will be only one value):
Expand|Select|Wrap|Line Numbers
  1. if myDict.has_key(au):
  2.     myDict[au] += area
  3. else:
  4.     myDict[au] = area
OR simpler still ;) :
Expand|Select|Wrap|Line Numbers
  1. myDict[au] = myDict.get(au, 0) + area
get() gives you the value of myDict[au], or 0 if it does not exist.
Jul 19 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by none | last post: by
9 posts views Thread by Yaroslav Bulatov | last post: by
7 posts views Thread by Hank | last post: by
1 post views Thread by john wright | last post: by
27 posts views Thread by Mark | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.