[Tutor] Dictionary - count values where values are stored as a list

GTXY20 gtxy20 at gmail.com
Tue Oct 2 01:25:17 CEST 2007


Thanks so much I changed to the following and this worked:

def HolderDistributionqty(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

M.

On 10/1/07, GTXY20 <gtxy20 at gmail.com> 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={}
>     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...
>
> M.
>
>
>
> On 10/1/07, Kent Johnson <kent37 at tds.net> wrote:
> >
> > GTXY20 wrote:
> > >
> > > This works perfectly.
> > >
> > > However I will be dealing with an import of a very large dictionary -
> > if
> > > I call the commands at command line this seems to be very taxing on
> > the
> > > CPU and memory and will take a long time.
> > >
> > > I was thinking of creating each as a fucntion whereby python would
> > just
> > > to write to a file instead of calling within a python shell do you
> > think
> > > that this would speed up the process?
> >
> > I don't understand what you are suggesting.
> >
> > Both of your requirements just need the values of the dict. If the dict
> > is being created from a file, you could probably build the count dicts
> > on the fly as you read the values without ever creating the dict with
> > all the items in it.
> >
> > Kent
> >
> > >
> > > All in total I will probably be looking at about 2 million dictionary
> > > keys with assorted value quantities.
> >
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20071001/1b8a8e96/attachment.htm 


More information about the Tutor mailing list