473,224 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,224 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 1586
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

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

Similar topics

1
by: none | last post by:
or is it just me? I am having a problem with using a dictionary as an attribute of a class. This happens in python 1.5.2 and 2.2.2 which I am accessing through pythonwin builds 150 and 148...
9
by: Yaroslav Bulatov | last post by:
I made an array of 10 million floats timed how long it takes to sum the elements, here's what I got (millis): gcc -O2: 21 Python with numarray: 104 Python with Numeric: 302...
2
by: SunMan | last post by:
Hello! I am trying to create a program that will ask for a user to enter a series of letters (codes) and then print out a table that shows the codes in decending frequency. Only letters will be...
2
by: Targa | last post by:
<input NAME="TAXRATE" onBlur="this.form.TAX.value = (this.form.TAXRATE.value - 0) * (this.form.ITEM1TOTAL.value - 0) + (this.form.ITEM2TOTAL.value - 0) " Size="4"> In my TAX field I get...
7
by: Hank | last post by:
I have a report-summing problem using Access 2000. When a section runs over the end of the page, sometimes a detail gets picked up twice. Example: Customer Header XYZ Company Detail Section...
1
by: john wright | last post by:
I have a dictionary oject I created and I want to bind a listbox to it. I am including the code for the dictionary object. Here is the error I am getting: "System.Exception: Complex...
2
by: MrL8Knight | last post by:
I am building a simple shopping cart and I am having problems trying to add the costs of the items to generate a total cost. I am new at this so forgive me if my technical verbiage isnít the...
12
by: neeraj | last post by:
Hi Can any body give me the syntax for summing the elements of the an array , without looping thanks
7
by: lethek39 | last post by:
Hey I have been trying to figure out how to sum rows and columns in a matrix square. I also have been trying to get the program to list the numbers of the diagonal in the matrix. So far this is the...
27
by: Mark | last post by:
Hi all, I have a scenario where I have a list like this: User Score 1 0 1 1 1 5 2 3 2 1
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.