打卡5

一.字典
1.字典基本操作
(1).dic= {‘python’:95,‘java’:99,‘c’:100}
print(len(dic)
(2).dic[‘java’]=98
print(dic)
(3).del dic[‘c’]
print(dic)
(4).dic[“php”]=90
(5).for key in dic:
print(key)
(6).for value in dic.value():
print(value)
(7).print(‘javascript’ in dic)
(8).print(sum(values))
(9).print(max(values))
(10).print(min(values))
(11).dic1 = {‘php’: 97}
dic.update(dic1)
print(dic)
2.字典中的value
def transfer_score(data):
for k in data.keys():
if type(data[k]) == dict:
for k1 in data[k].keys():
if type(data[k][k1]) == list:
for j in range(len(data[k][k1])):
data[k][k1][j] = int(data[k][k1][j])
elif type(data[k][k1]) == dict:
for k2 in data[k][k1].keys():
data[k][k1][k2] = int(data[k][k1][k2])
else:
data[k][k1] = int(data[k][k1])
elif type(data[k])==list:
for i in range(len(data[k])):
if type(data[k][i]) == list:
for m in range(len(data[k][i])):
data[k][i][m] = int(data[k][i][m])
elif type(data[k][i]) == dict:
for k3 in data[k][i].keys():
data[k][i][k3] = int(data[k][i][k3])
else:
data[k][i] = int(data[k][i])
return data
data = {
‘python’: {‘上学期’: ‘90’, ‘下学期’: ‘95’},
‘c++’: [‘95’, ‘96’, ‘97’],
‘java’: [{‘月考’:‘90’, ‘期中考试’: ‘94’, ‘期末考试’: ‘98’}]
}
print(transfer_score(data))
二.集合
1.x = (1,)
2.s = set()
s.update( {‘x’,‘y’,‘z’})
3.a = [‘A’, ‘B’, ‘A’, ‘B’]
s = set(a)
b = list(s)
4.a = {6, 7, 8}
b = {7, 8, 9}
(a | b) - (a & b)
5.a = {‘A’, ‘B’, ‘C’}
b = {‘B’, ‘C’, ‘D’}
cnt = 0
for item in a:
if item in b:
cnt += 1
print(cnt)
三.序列
1.a = {‘1’, ‘2’, ‘3’}
b= [‘B’, ‘C’, ‘D’]
print(max(a),max(b))
print(min(a),min(b))
2.sort 是应用在 list 上的方法,属于列表的成员方法,sorted 可以对所有可迭代的对象进行排序操作;list 的 sort 方法返回的是对已经存在的列表进行操作,而内建函数 sorted 方法返回的是一个新的 list,而不是在原来的基础上进行的操作;sort使用方法为ls.sort(),而sorted使用方法为sorted(ls)
3.x = range(100)
print(sum(x))
4.x = [2,3,4,5]
x = [i ** 3 for i in x]
print(x)
5.x1 = [‘x’,‘y’,‘z’]
x2 = [1,2,3]
b = {x1[i]:x2[i] for i in range(len(x1))}
print(b)

©️2020 CSDN 皮肤主题: 1024 设计师:上身试试 返回首页