braver wrote:
> ...
> The real-life motivation for this is n-gram counting. Say you want to
> maintain a hash for bigrams. For each two subsequent words a, b in a
> text, you do
> bigram_count[a][b] += 1
This application is easily handed with tuples as keys.
bigrams = {}
src = iter(so
Here's a working version of the ngram counter with nested dict, wonder
how it can be improved!
lines = ["abra ca dabra",
"abra ca shvabra",
"abra movich roman",
"abra ca dabra",
"a bra cadadra"]
ngrams = [x.split() for x in lines]
N = 3
N1 = N-1
orig = {}
for ng
On Tue, 13 Nov 2007 08:02:08 -0800, braver wrote:
> Greetings: I wonder how does one uses single-name variables to refer
> to nested sunhashes (subdictionaries). Here's an example:
That's possible and you do it in your example.
> In [41]: orig = { 'abra':{'foo':7, 'bar':9}, 'ca':{}, 'dabra':{'b
Greetings: I wonder how does one uses single-name variables to refer
to nested sunhashes (subdictionaries). Here's an example:
In [41]: orig = { 'abra':{'foo':7, 'bar':9}, 'ca':{}, 'dabra':{'baz':
4} }
In [42]: orig
Out[42]: {'abra': {'bar': 9, 'foo': 7}, 'ca': {}, 'dabra': {'baz': 4}}
In [43]: