Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 a dictionary with a key default
#1
i can use the method .get() on a dictionary to use a single specific default. i would like to have a simple means like that which can be inserted into an expression which will return the value of the key itself if there is no matching key in the dictionary. i do not want something that requires expressing the key twice, so i can use costly key expressions and/or key expressions with side effects.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#2
express the key on separate line and assign to name, then use that name twice in get() call or define custom class that inherit from dict but override get() method and use it instead of built in class
Quote
#3
More often that not I have trouble grasping the question. There is side effect of adding new key value pair but anyways:

>>> key = 'oh my'                                                   
>>> d = {'meaning of life': 42}                                     
>>> d.setdefault(key, key)                                          
'oh my'
>>> d.setdefault('meaning of life', 'no way')                       
42
>>> d                                                               
{'meaning of life': 42, 'oh my': 'oh my'}
With another side effect:

>>> key = 'oh no'                                                   
>>> d = {'meaning of life': 42}                                     
>>> d.pop(d.setdefault(key, key))                                   
'oh no'
>>> d                                                               
{'meaning of life': 42}
>>> d.pop(d.setdefault('meaning of life', 'oh no'))                 
--------------------------------------------------------------------------
/.../
----> 1 d.pop(d.setdefault('meaning of life', 'oh no'))
KeyError: 42
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Quote
#4
i was wanting to put this as an expression inside a comprehension.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#5
the dictionary is mapping some aliases for some strings used in some comprehensions.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)