473,569 Members | 2,691 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

KeyError: 3 problem

GTXY20
29 New Member
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 12850
ilikepython
844 Recognized Expert Contributor
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 Recognized Expert Expert
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 New Member
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
1613
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 program = """ abc = {}
1
5305
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 = tablesDirectoryPrefix + os.environ File "C:\Python23\lib\os.py", line 417, in __getitem__ return self.data KeyError: 'REMOTE_ADDR'
3
5093
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 def createData():
117
7113
by: Peter Olcott | last post by:
www.halting-problem.com
28
5183
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(); .... and then call the virtual method, why is it that the base class's method is called instead of the overridden method? How do I fix this if I...
4
8034
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
3964
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 "/Library/Frameworks/Python.framework/Versions/2.4//lib/python2.4/urllib.py", line 1117, in quote res = map(safe_map.__getitem__, s)
2
7833
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 loaded. The traceback is: File "pickle.pyo", line 1374, in loads File "pickle.pyo", line 858, in load
6
55543
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 the quiz or quizes that you wish to use.', defaultDir=os.getcwd(), defaultFile='',
0
7612
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7922
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8119
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7668
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5218
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2111
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.