joquius Posted May 30, 2008 Share Posted May 30, 2008 Mehh. I'm just about ready to kick the ass of the guy who wrote the python manual. Why is there no obvious list/dictionary method list? Why is there no proper search? Anyone know what the python equivalent of PHP's array_search is? Basically trying to get a key according to a value, rather than value according to a key. Quote Link to comment https://forums.phpfreaks.com/topic/108032-solved-python-equivalent-to-php-array_search/ Share on other sites More sharing options...
tomfmason Posted May 31, 2008 Share Posted May 31, 2008 you could do something like this def find_key(dic, val): return [k for k, v in dic.iteritems() if v == val][0] and use it like foo = { 'key1' : 'value1', 'key2' : 'value2' } print find_key(foo, 'value1') # prints key1 Quote Link to comment https://forums.phpfreaks.com/topic/108032-solved-python-equivalent-to-php-array_search/#findComment-554478 Share on other sites More sharing options...
joquius Posted June 1, 2008 Author Share Posted June 1, 2008 Nice solution. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/108032-solved-python-equivalent-to-php-array_search/#findComment-554732 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.