referencing a subhash for generalized ngram counting

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Tue Nov 13 17:17:46 CET 2007


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':{'baz':
> 4} }
> 
> In [42]: orig
> Out[42]: {'abra': {'bar': 9, 'foo': 7}, 'ca': {}, 'dabra': {'baz': 4}}
> 
> In [43]: h = orig['ca']

Here you are getting the reference to the empty dictionary and bind it to
the name `h`.

> In [44]: h = { 'adanac':69 }
>
> In [45]: h
> Out[45]: {'adanac': 69}
> 
> In [46]: orig
> Out[46]: {'abra': {'bar': 9, 'foo': 7}, 'ca': {}, 'dabra': {'baz': 4}}
> 
> I want to change orig['ca'], which is determined somewhere else in a
> program's logic, where subhashes are referred to as h -- e.g., for x
> in orig: ... .  But assigning to h doesn't change orig.

Correct.  Changing the dictionary bound to `h` changes it::

  h = orig['ca']
  h['adanac'] = 69
 
> Yet since names are not exactly references, something else is needed
> for generalized ngram multi-level counting hash -- what?

Names *are* implemented as references to objects, but binding the name to
a different object has no effect on the object bound to that name before.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list