简体   繁体   中英

Problems with pass-by-reference in Python 2d list comprehensions

Take this dict of dicts:

case_forms = {'plural': {'nominative': 'dni', 'locative': 'dniach'}, 
              'singular': {'instrumental': 'dniem', 'vocative': 'dzie\xc5\x84'}}

I'd like to get a list of all the (a,b) key-pairs that are usable as case_forms[a][b] .

No problem, right? Double list comprehension. Do stuff like that all the time in Haskell:

[(number, case_name) for case_name in case_dict.keys() for number, case_dict in case_forms.items()]

Except this doesn't produce the result you would expect:

[('plural', 'instrumental'),
 ('singular', 'instrumental'),
 ('plural', 'vocative'),
 ('singular', 'vocative')]

I'm wondering how to fix this problem. No amount of cleverly placed [:] seems to do the trick.

How about this :

[ (number, case_name) for number, case_dict in case_forms.items() for case_name in case_dict.keys() ]

Edited to reference @juanpa.arrivillaga's comments about why my example was behaving strangely:

Python 2 list comprehensions have leaky scope, and it's not using the case_dict you think it is, it's using the case_dict from a previous comprehension (that leaked into the external scope)

Start a new interpreter session and you'll get the NameError

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM