from collections import OrderedDict
od=OrderedDict()
od['a']=1
od['c']=2
od['b']=3
print(od)
OrderedDict([('a', 1), ('c', 2), ('b', 3)])
list(od.keys())
['a', 'c', 'b']
keys=['apple','banana','orange']
values=[4,5,6]
od.update(zip(keys,values))
print(od)
OrderedDict([('a', 1), ('c', 2), ('b', 3), ('apple', 4), ('banana', 5), ('orange', 6)])
od.pop('a')
1
print(od)
OrderedDict([('c', 2), ('b', 3), ('apple', 4), ('banana', 5), ('orange', 6)])
od.move_to_end('b')
print(od)
OrderedDict([('c', 2), ('apple', 4), ('banana', 5), ('orange', 6), ('b', 3)])
from collections import defaultdict
dic0=defaultdict(lambda:'N/A')
dic0['key1']=1
dic0['key1']
1
dic0['key2']
'N/A'
list1=['a','b','d','a','a','d','c','e','d','a','f','e']
list1_count={
}
for i in list1:
if i not in list1_count:
list1_count[i]=1
else:
list1_count[i]+=1
print(list1_count)
{'a': 4, 'b': 1, 'd': 3, 'c': 1, 'e': 2, 'f': 1}
from collections import Counter
list2=['a','b','d','a','a','d','c','e','d','a','f','e']
a=Counter(list2)
print(a)
print(dict(a))
Counter({'a': 4, 'd': 3, 'e': 2, 'b': 1, 'c': 1, 'f': 1})
{'a': 4, 'b': 1, 'd': 3, 'c': 1, 'e': 2, 'f': 1}
print(a.most_common(2))
[('a', 4), ('d', 3)]
a.most_common(1)[0][1]
4
from datetime import datetime
time=datetime.now()
print(time)
2020-09-17 16:09:17.750805
type(time)
datetime.datetime
from datetime import datetime
date=datetime(2020,12,31,10,59)
print(date)
2020-12-31 10:59:00
date.year
2020
date.month
12
date.day
31
date.hour
10
date.minute
59
from datetime import datetime
dt=datetime.now()
dt.timestamp()
1600330584.88398
from datetime import datetime
oneday=datetime.strptime('2020-10-30 11:00:30','%Y-%m-%d %H:%M:%S')
print(oneday)
2020-10-30 11:00:30
from datetime import time
time=datetime.now()
time.strftime('%Y')
'2020'
from datetime import datetime
present=datetime.now()
birth=datetime(1994,3,29,16,45)
age=present-birth
print(age)
9668 days, 23:43:12.117464
from datetime import datetime,timedelta
list1=['2020-10-20','1988-01-27']
day1=datetime.strptime(list1[0],'%Y-%m-%d')
day2=datetime.strptime(list1[1],'%Y-%m-%d')
gap=day1-day2
print(gap.days)
11955
import json
data={
'name':'ACME','shares':100,'price':542.23}
json_str=json.dumps(data)
print(json_str)
{"name": "ACME", "shares": 100, "price": 542.23}
data1=json.loads(json_str)
print(data1)
{'name': 'ACME', 'shares': 100, 'price': 542.23}
type(data)
dict
type(data1)
dict
import json
data2=[{
'a':1,'b':2,'c':3,'d':4}]
with open('data.json','w') as f:
json.dump(data2,f)
data3=[{
'May':50,'Jane':60,'July':65,'August':78,'September':80}]
with open('data3.json','w') as m:
json.dump(data3,m)
with open('data3.json','r') as n:
data4=json.load(n)
print(data4)
[{'May': 50, 'Jane': 60, 'July': 65, 'August': 78, 'September': 80}]
import random
num=random.random()
print(num)
0.6841113691590242
import random
random.uniform(10,50)
29.48766674134876
random.uniform?
random.randint(10,50)
37
for i in range(1,10,2):
print(i,end=',')
1,3,5,7,9,
import random
random.randrange(1,10,2)
5
list=['a','b','c']
import random
random.choice(list)
'b'
random.choice?
from random import choices
mylover=[33,44,55,66]
choices(mylover,k=2)
[55, 55]
from random import sample
enemy=[99,88,77]
sample(enemy,2)
[77, 88]
from random import shuffle
lst=['nancy','amy','hebe','lily','sylvia']
shuffle(lst)
print(lst)
['hebe', 'sylvia', 'lily', 'nancy', 'amy']