dictionary idiom needed

B

Brandon

Hi all,

I have a series of lists in format ['word', 'tagA', 'tagB']. I have
converted this to a few dicts, such as one in which keys are tuples of
('word', 'tagB'), and the values are the number of times that key was
found. I need an dictionary idiom whereby I can find all instances of
a given 'word' with any 'tagB', and then subdivide into all instances
of a given 'tagB'. In both cases I would want the value as a count of
all instances found. Can this be done with dictionaries? Or should I
back up and do this with lists? All of the nested for loops I have
tried return replicated results, so I can't trust those values.

Thanks for any pointers,

Brandon
 
B

bearophileHUGS

Brandon:
I need an dictionary idiom whereby I can find all instances of
a given 'word' with any 'tagB', and then subdivide into all instances
of a given 'tagB'.  In both cases I would want the value as a count of
all instances found.

If I have understood you well enough, I think you can do with a dict
that has the tuple ('word', 'tagB') as key, and as value has a
collections.defaultdict(int) that maps 'tagB' to its count.

Bye,
bearophile
 
B

bearophileHUGS

bearophile:
you can do with a dict
that has the tuple ('word', 'tagB') as key, and as value has a
collections.defaultdict(int) that maps 'tagB' to its count.

Where's 'tagA'?
Probably I haven't understood your problem well enough. I need a
better example of your data and what you need...

Sorry, bye,
bearophile
 
B

Brandon

Thanks bear -

Some outside advice has me looking at nested dictionaries. But I am
still bogged down because I've not created one before and all examples
I can find are simple ones where they are created manually, not with
loops. Maybe a further example:

data:
POS1 POS2 POS3
['word1','tagA','tagB']
['word2','tagC','tagD']
['word1','tagE','tagB']
['word1','tagC','tagF']

.... and so on. FWIW: I am guaranteed that the set of tags that may
occur in position2 is complementary to the set of tags that may occur
in position3.

Now I want to get an accounting of all the tags that occurred in
position3 in the context of, say, word1. Here I've shown that for
word1, tagB and tagF occurs. I want a way to access all this
information such that nested_dict['word1']['tagB'] = 2, and nested_dict
['word1']['tagF'] = 1.

As I mentioned, I already have dicts such that dictA['word1'] = 3, and
dictB['tagB'] = 2. I used defaultdict to build those, and that seems
to be causing me to get some funky values back from my initial
attempts to build a nested dictionary. I am stumbling at constructing
a "for" loop that automatically creates such nested dictionaries.

I hope that clears things up. If you still have any advice, it's much
appreciated.

Thanks,

Brandon
 
B

Brandon

Smells like homework without a particular application.

@Scott:

Even if that were the case :) I'd still like to figure out how to
create nested dictionaries!

Brandon
 
A

Arnaud Delobelle

Brandon said:
Thanks bear -

Some outside advice has me looking at nested dictionaries. But I am
still bogged down because I've not created one before and all examples
I can find are simple ones where they are created manually, not with
loops. Maybe a further example:

data:
POS1 POS2 POS3
['word1','tagA','tagB']
['word2','tagC','tagD']
['word1','tagE','tagB']
['word1','tagC','tagF']

... and so on. FWIW: I am guaranteed that the set of tags that may
occur in position2 is complementary to the set of tags that may occur
in position3.

Now I want to get an accounting of all the tags that occurred in
position3 in the context of, say, word1. Here I've shown that for
word1, tagB and tagF occurs. I want a way to access all this
information such that nested_dict['word1']['tagB'] = 2, and nested_dict
['word1']['tagF'] = 1.

As I mentioned, I already have dicts such that dictA['word1'] = 3, and
dictB['tagB'] = 2. I used defaultdict to build those, and that seems
to be causing me to get some funky values back from my initial
attempts to build a nested dictionary. I am stumbling at constructing
a "for" loop that automatically creates such nested dictionaries.

I hope that clears things up. If you still have any advice, it's much
appreciated.

Thanks,

Brandon
from collections import defaultdict

data = [['word1','tagA','tagB'],
.... ['word2','tagC','tagD'],
.... ['word1','tagE','tagB'],
.... ['word1','tagC','tagF']].... d[w][t2] += 1
....
d['word1']['tagB'] 2
d['word2']['tagD'] 1
d['word2']['tagF']
0

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top