Created
February 17, 2010 12:23
-
-
Save sneeu/306550 to your computer and use it in GitHub Desktop.
What I thought was the first non-trivial example I'd found of PHP being shorter than Python. Saved by Twitter.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
/** | |
* getValues(array('a' => 1, 'b' => 2), array('b', 'c')); # => array('b' => 2) | |
*/ | |
function getValues($data, $fields) | |
{ | |
return array_intersect_key($data, array_flip($fields)); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def values(data, fields): | |
""" | |
>>> values({'a': 1, 'b': 2}, ['b', 'c']) | |
{'b': 2} | |
""" | |
return dict((k, data[k]) for k in fields if k in data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment