473,231 Members | 1,382 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,231 software developers and data experts.

KeyError: 3 problem

GTXY20
29
Hello,

Any comments on the below mentioned problem are most appreciated.

I have a function and it is throwing this error:

Expand|Select|Wrap|Line Numbers
  1. FEXpython_v2.py", line 32, in UnitHolderDistributionqty
  2.     count[item]+=1
  3. KeyError: 3
This is the function:

Expand|Select|Wrap|Line Numbers
  1. def Distributionqty(dictionary):
  2.     holder=list()
  3.     held=list()
  4.     distqtydic={}
  5.     count={}
  6.     for key in sorted(dictionary.keys()):
  7.         holder.append(key)
  8.         held.append(len(dictionary[key]))
  9.     for (key, value) in map(None, holder, held):
  10.         distqtydic[key]=value
  11.     for item in distqtydic.values():
  12.         count[item]+=1
  13.     for k,v in sorted(count.items()):
  14.         fdist=k
  15.         qty=v
  16.         print fdist,qty
Not sure why.

G.
Oct 1 '07 #1
3 12813
ilikepython
844 Expert 512MB
Hello,

Any comments on the below mentioned problem are most appreciated.

I have a function and it is throwing this error:

Expand|Select|Wrap|Line Numbers
  1. FEXpython_v2.py", line 32, in UnitHolderDistributionqty
  2.     count[item]+=1
  3. KeyError: 3
This is the function:

Expand|Select|Wrap|Line Numbers
  1. def Distributionqty(dictionary):
  2.     holder=list()
  3.     held=list()
  4.     distqtydic={}
  5.     count={}
  6.     for key in sorted(dictionary.keys()):
  7.         holder.append(key)
  8.         held.append(len(dictionary[key]))
  9.     for (key, value) in map(None, holder, held):
  10.         distqtydic[key]=value
  11.     for item in distqtydic.values():
  12.         count[item]+=1
  13.     for k,v in sorted(count.items()):
  14.         fdist=k
  15.         qty=v
  16.         print fdist,qty
Not sure why.

G.
You need something like this:
Expand|Select|Wrap|Line Numbers
  1. for item in distqtydic.values():
  2.     if distqtydic.has_key(item):
  3.         count[item]+=1
  4.     else:
  5.         count[item] = 1
  6.  
Oct 1 '07 #2
bartonc
6,596 Expert 4TB
Going back to your first lesson, your keys are strings (as in '3'), if you haven't changed things too much since then. In that case, you'd use:
Expand|Select|Wrap|Line Numbers
  1. count[str(item)]+=1
but I don't recall at this moment if the what type target value is. If it is an int, it may or may not (don't recall) be passed by reference. If you can give a sample of the argument, that would help a lot.
Oct 1 '07 #3
GTXY20
29
Hi.

I was able to work out with the following:

Expand|Select|Wrap|Line Numbers
  1. def UnitHolderDistributionqty(dictionary):
  2. from collections import defaultdict
  3.     count=defaultdict(int)
  4.     for item in dictionary.values():
  5.         count[len(item)]+=1
  6.     for k,v in sorted(count.items()):
  7.         fdist=k
  8.         qty=v
  9.         print fdist,qty
Thanks again.
Oct 1 '07 #4

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

Similar topics

0
by: Norman Shelley | last post by:
Is there a way to get at the object one is trying to access with a key to list out all the possible keys? See the except clause below. #!/usr/bin/env python2.2 from inspect import * import sys...
1
by: | last post by:
Hello. Maybe someone will help me with this KeyError: ............................. Traceback (most recent call last): File "C:\Python\tabla.py", line 929, in -toplevel- tablesDirectory =...
3
by: Rune Strand | last post by:
I'm experiencing strange errors both with pickle and cPickle in the below code: import cPickle as pickle #import pickle from string import ascii_uppercase from string import ascii_lowercase...
117
by: Peter Olcott | last post by:
www.halting-problem.com
28
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class... BaseClass bc = new ChildClass();...
4
by: homepricemaps | last post by:
i have an href which looks like this: <td class="all"> <a class="btn" name="D1" href="http://www.cnn.com"> </a> here is my code
3
by: nyenyec | last post by:
urllib.quote chokes on unicode in 2.4.4. 2.4.4 (#1, Oct 18 2006, 10:34:39) Traceback (most recent call last): File "<stdin>", line 1, in ? File...
2
by: christof | last post by:
Hi, I am using pickle/unpickle to let my program save its documents to disk. While this it worked stable for a long time, one of my users now complained, that he had a file which can't be...
6
by: josh001 | last post by:
I'm trying to figure out why KeyError: 0 pops up when I try to run the program containing the code. OpenDLG = wx.FileDialog( self.win, message='Please choose...
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...
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: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
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
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.