471,330 Members | 1,871 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,330 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 12420
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

Post your reply

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

Similar topics

reply views Thread by Norman Shelley | last post: by
1 post views Thread by | last post: by
3 posts views Thread by Rune Strand | last post: by
117 posts views Thread by Peter Olcott | last post: by
28 posts views Thread by Jon Davis | last post: by
4 posts views Thread by homepricemaps | last post: by
2 posts views Thread by christof | last post: by
reply views Thread by rosydwin | last post: by

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.