Am 21.09.2012 00:58, schrieb thorso...@lavabit.com:
list = [{'1': []}, {'2': []}, {'3': ['4', '5']}]
I want to check for a value (e.g. '4'), and get the key of the dictionary
that contains that value.
Note:
1. list is a built-in type, who's name is rebound above
2. The list above contains dictionaries that all only contain a single key?
3. You have strings containing decimal representations of numbers?
> (Yep, this is bizarre.)
The data are really stored in a strange way and you might be able to
make things clearer by reorganizing them a bit.
some_magic(list, '4')
=> '3'
What's the functional way to do it?
Functional as in functional programming and an emphasis on lazy
evaluation? In that case I'd write a generator that emits the keys where
the values contain the requested string.
Is it possible to do it with a one-liner?
Yep, filter(), lambda and the 'in' operator. Question remains if this is
readable. Note that you can use a local function, too, if you just want
to reduce the scope/visibility.
Good luck!
Uli
--
http://mail.python.org/mailman/listinfo/python-list