473,406 Members | 2,956 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,406 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 1592
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
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.