KeyError: 3 problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • GTXY20
    New Member
    • Oct 2007
    • 29

    KeyError: 3 problem

    Hello,

    Any comments on the below mentioned problem are most appreciated.

    I have a function and it is throwing this error:

    Code:
    FEXpython_v2.py", line 32, in UnitHolderDistributionqty
        count[item]+=1
    KeyError: 3
    This is the function:

    Code:
    def Distributionqty(dictionary):
        holder=list()
        held=list()
        distqtydic={}
        count={}
        for key in sorted(dictionary.keys()):
            holder.append(key)
            held.append(len(dictionary[key]))
        for (key, value) in map(None, holder, held):
            distqtydic[key]=value
        for item in distqtydic.values():
            count[item]+=1
        for k,v in sorted(count.items()):
            fdist=k
            qty=v
            print fdist,qty
    Not sure why.

    G.
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by GTXY20
    Hello,

    Any comments on the below mentioned problem are most appreciated.

    I have a function and it is throwing this error:

    Code:
    FEXpython_v2.py", line 32, in UnitHolderDistributionqty
        count[item]+=1
    KeyError: 3
    This is the function:

    Code:
    def Distributionqty(dictionary):
        holder=list()
        held=list()
        distqtydic={}
        count={}
        for key in sorted(dictionary.keys()):
            holder.append(key)
            held.append(len(dictionary[key]))
        for (key, value) in map(None, holder, held):
            distqtydic[key]=value
        for item in distqtydic.values():
            count[item]+=1
        for k,v in sorted(count.items()):
            fdist=k
            qty=v
            print fdist,qty
    Not sure why.

    G.
    You need something like this:
    [code=python]
    for item in distqtydic.valu es():
    if distqtydic.has_ key(item):
    count[item]+=1
    else:
    count[item] = 1
    [/code]

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      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:[CODE=python]count[str(item)]+=1[/CODE]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.

      Comment

      • GTXY20
        New Member
        • Oct 2007
        • 29

        #4
        Hi.

        I was able to work out with the following:

        Code:
        def UnitHolderDistributionqty(dictionary):
        from collections import defaultdict
            count=defaultdict(int)
            for item in dictionary.values():
                count[len(item)]+=1
            for k,v in sorted(count.items()):
                fdist=k
                qty=v
                print fdist,qty
        Thanks again.

        Comment

        Working...