Ulrich Eckhardt於 2012年9月21日星期五UTC+8下午5時15分03秒寫道: > 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
I don't think functional aspects are only marked as lazy programming. It just means when one is experimenting something the efficient execution in speed is not on focus yet. -- http://mail.python.org/mailman/listinfo/python-list