Hi Kent,

Yes I think so I think I am almost there with this:

from collections import defaultdict
d = {'1': ['220', '220', '220''], '2': ['220', '238', '238', '238', '238'],
'3': ['220', '238'], '4': ['220', '220'], '5': ['220', '220', '238'], '6':
['238', '238'], '7': ['220']}

for f, b in d.items():
    h = defaultdict(int)
    for j in b:
        h[j]+=1
    print ('%s, %s' % (f, h))

However, not exactly happy with the printed output as soon as I complete I
will repost what I come up with.

Thanks so much.

M.

On Tue, Apr 15, 2008 at 10:17 PM, Kent Johnson <[EMAIL PROTECTED]> wrote:

> GTXY20 wrote:
>
> >
> > Hi tutors,
> >
> > I currently have a dictionary like the following:
> >
> > {'1': ['220', '220', '220''], '2': ['220', '238', '238', '238', '238'],
> > '3': ['220', '238'], '4': ['220', '220'], '5': ['220', '220', '238'], '6':
> > ['238', '238'], '7': ['220']}
> >
> > I am trying to create a dictionary that would list the current key and a
> > count of the iterations of values within the value list like so:
> >
> > {'1': {'220' : 3}, '2': {'220' : 1}, 2: {238 : 4}, '3': {'220' : 1}, 3:
> > { '238' : 1}, '4': {220 : 2}, '5': {'220: 2}, '5': {238 : 1}, '6': {'238' :
> > 2}, '7': {'220' : 1}}
> >
>
> ?? Do you really want keys of '2' and 2? How can you have two keys '5'? I
> guess maybe you want
> {'1': {'220' : 3}, '2': {'220' : 1, 238 : 4}, '3': {'220' : 1, '238' : 1},
> '4': {220 : 2}, '5': {'220: 2, 238 : 1}, '6': {'238' : 2}, '7': {'220' : 1}}
>
>
> Now I am pretty sure that I need to loop through the first dictionary and
> > create a defaultdict from the values for each key in that dictionary but at
> > this point I am having difficulty coming up with the loop.
> >
> > I am looking for a satrting point or any suggestions.
> >
>
> Do you know how to turn
> ['220', '238', '238', '238', '238']
> into
> {'220' : 1, '238' : 4}
> ?
>
> If so, then put that code in a loop over the key, value pairs of the dict.
>
> Kent
>
_______________________________________________
Tutor maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/tutor

Reply via email to