Python学习Task05:字典、集合和序列

字典

Python学习Task05:字典、集合和序列_第1张图片
Python学习Task05:字典、集合和序列_第2张图片


1.

#代码
dic = {
    'python': 95,
    'java': 99,
    'c': 100
    }
print("字典的长度是:%d" % len(dic))
dic['java'] = 98
print(dic)
dic.pop('c')
print(dic)
dic.setdefault('php',90)
print(dic)
print(list(dic.keys()))
print(list(dic.values()))
print('javascript' in dic)
print(sum(dic.values()))
print(max(dic.values()))
print(min(dic.values()))
dic1 = {'php':97}
dic.update(dic1)
print(dic)
    
#结果
字典的长度是:3
{'python': 95, 'java': 98, 'c': 100}
{'python': 95, 'java': 98}
{'python': 95, 'java': 98, 'php': 90}
['python', 'java', 'php']
[95, 98, 90]
False
283
98
90
{'python': 95, 'java': 98, 'php': 97}
data = {
        'python': {'上学期': '90', '下学期': '95'},
        'c++': ['95', '96', '97'],
        'java': [{'月考':'90', '期中考试': '94', '期末考试': '98'}]
        }
def transfer_score(data):
    data['python']['上学期']=int(data['python']['上学期'])
    data['python']['下学期']=int(data['python']['下学期'])
    data['c++'][0]=int(data['c++'][0])
    data['c++'][1]=int(data['c++'][1])
    data['c++'][2]=int(data['c++'][2])
    data['java'][0]['月考']=int(data['java'][0]['月考'])
    data['java'][0]['期中考试']=int(data['java'][0]['期中考试'])
    data['java'][0]['期末考试']=int(data['java'][0]['期末考试'])
    print(data)
transfer_score(data)
#{'python': {'上学期': 90, '下学期': 95}, 'c++': [95, 96, 97], 'java': [{'月考': 90, '期中考试': 94, '期末考试': 98}]}

集合Python学习Task05:字典、集合和序列_第3张图片

答:
1.

x = (1,)
type(x)
#
x = set()
x.add('x')
x.add('y')
x.add('z')
print(x)
#{'x', 'y', 'z'}
x = ['A','B','A','B']
y = set(x)
print(list(y))
#['B', 'A']
a = {6,7,8}
b = {7,8,9}
c = a.difference(b)
d = b.difference(a)
print('两个集合中不重复的元素是:',c,d)
#两个集合中不重复的元素是: {6} {9}
a = {'A','B','C'}
b = {'B','C','D'}
time = 0
for i in a:
    if i in b:
        time+=1
print('出现的次数:%d' % time)
#出现的次数:2

序列Python学习Task05:字典、集合和序列_第4张图片

答:
1.
max( ) 返回序列或参数集合中的最大值
min( ) 返回序列或参数集合中的最小值
2.
sort( )对序列进行永久性排序,sorted( )对序列进行临时排序
3.
sum(range(101))
4.

lst = [2,3,4,5]
for i in range(len(lst)):
    lst[i]=pow(lst[i],1/3)
print(lst)
#[1.2599210498948732, 1.4422495703074083, 1.5874010519681994, 1.7099759466766968]
a = ['x','y','z']
b = [1,2,3]
c = list(zip(a,b))
print(c)
#[('x', 1), ('y', 2), ('z', 3)]

你可能感兴趣的:(Python学习Task05:字典、集合和序列)