GTXY20 wrote:
>  
> Thanks again I have worked that issue out.
>  
> However I have the following function and it is throwing this error:
>  
> FEXpython_v2.py", line 32, in UnitHolderDistributionqty
>     count[item]+=1
> KeyError: 3
>  
> This is the function:
>  
> def Distributionqty(dictionary):
>     holder=list()
>     held=list()
>     distqtydic={}
>     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

Unless you need holder and held for something else, all of the above can 
be replaced with
   distqtydic = dict( (key, len(value)) for key, value in 
sorted(dictionary.items())

>     count={}
>     for item in distqtydic.values():
>         count[item]+=1

The problem is that you are trying to add 1 to a value that is not 
defined yet. You can either make
   count=defaultdict(int)
or write
   count[item] = count.get(item, 0) + 1

If this is the only use of distqtydic then even the shortened code above 
is more than you need, you could just write

   for item in dictionary.values():
     count[len(item)] += 1      # assuming defaultdict

Kent

>     for k,v in sorted(count.items()):
>         fdist=k
>         qty=v
>         print fdist,qty

   for fdist,qty in sorted(count.items()):
     print fdist, qty

No need for the intermediate k, v; just use the names you like.

Kent

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to