Skip to content

Instantly share code, notes, and snippets.

@arischow
Last active May 4, 2019 01:43
Show Gist options
  • Save arischow/42529b7984a8dbc8b9b202eaa0fda88e to your computer and use it in GitHub Desktop.
Save arischow/42529b7984a8dbc8b9b202eaa0fda88e to your computer and use it in GitHub Desktop.
Python
import random
# 生成5个0~9的不重复随机数
print random.sample(range(10), 5)
# [7, 9, 6, 2, 3]
# 从a~d中取出不重复的三个字母
print random.sample(['a', 'b', 'c', 'd'], 3)
# ['d', 'b', 'c']
# http://stackoverflow.com/a/8381589/4003204
### Making a dictionary ###
data = {}
# OR
data = dict()
### Initially adding values ###
data = {'a':1,'b':2,'c':3}
# OR
data = dict(a=1, b=2, c=3)
### Inserting/Updating value ###
data['a']=1 # updates if 'a' exists, else adds 'a'
# OR
data.update({'a':1})
# OR
data.update(dict(a=1))
# OR
data.update(a=1)
### Merging 2 dictionaries ###
data.update(data2) # Where data2 is also a dict.
### Deleting items in dictionary ###
del data[key] #Remove specific element in a dictionary
data.pop(key) #Removes the key & returns the value
data.clear() #Clear entire dictionary
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment