Last active
March 9, 2018 03:32
-
-
Save 92hackers/c0a63f8372024c9e76286fdcaa20fc4e to your computer and use it in GitHub Desktop.
Python Snippets
This file contains 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
# 1: sort a list | |
data = [{'cid': 32, 'haha': 'nice'}, | |
{'cid': 31, 'haha': 'nice'}, | |
{'cid': 30, 'haha': 'nice'}, | |
{'cid': 29, 'haha': 'nice'}, | |
{'cid': 28, 'haha': 'nice'}, | |
{'cid': 27, 'haha': 'nice'}, | |
{'cid': 26, 'haha': 'nice'}, | |
{'cid': 25, 'haha': 'nice'}, | |
{'cid': 24, 'haha': 'nice'}, | |
{'cid': 23, 'haha': 'nice'}, | |
{'cid': 22, 'haha': 'nice'}, | |
{'cid': 21, 'haha': 'nice'}] | |
def get_cid(item): | |
return item.get('cid') | |
sorted(data, key=get_cid, reverse=True) # reverse param means 倒序。:) | |
# 2: Delete a key from a dict | |
map = {'name': 'cy', 'age': 26} | |
filtered_data = {k: v for k, v in map.items() if k != 'age'} | |
del map['name'] | |
map.pop('name', None) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment