473,385 Members | 1,673 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,385 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 12831
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.