Skip to content

Instantly share code, notes, and snippets.

@92hackers
Last active March 9, 2018 03:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 92hackers/c0a63f8372024c9e76286fdcaa20fc4e to your computer and use it in GitHub Desktop.
Save 92hackers/c0a63f8372024c9e76286fdcaa20fc4e to your computer and use it in GitHub Desktop.
Python Snippets
# 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