作业题1 :
解答:
lis = [4,-2,3,1]
lis2 = sorted(lis,key = abs)
print(lis2)
结果:
[1, -2, 3, 4]
作业题2:
解答:
infors = [{'name':'qian','age':28},{'name':'amy','age':20},{'name':'james','age':25}]
a = sorted(infors,key = lambda e:e.__getitem__('name'))
print(a)
结果:
[{'name': 'amy', 'age': 20}, {'name': 'james', 'age': 25}, {'name': 'qian', 'age': 28}]
作业题3:
解答:
import numpy as np
def agvCount(lis):
agv = np.mean(lis)
return agv
def sumCount():
a,b,c = input('请输入三个数字,用逗号隔开:').split(',')
# 对用户输入的三个数字进行求和
s = float(a) + float(b) + float(c)
lis = [float(a) , float(b) , float(c) ]
agv = agvCount(lis)
return s,agv
s,agv = sumCount()
# print(agv)
print(f'三个数字的和为:{s},平均值为:{agv}')
结果:
请输入三个数字,用逗号隔开:5.2,6.5,86.5
三个数字的和为:98.2,平均值为:32.733333333333334
作业题4:
解答:
l = ["jack",("tom",23),"rose",(14,55,67)]
def printEverything(l):
if isinstance(l, (int,str)):
print(l)
else:
for i in l:
printEverything(i)
printEverything(l)
结果:
jack
tom
23
rose
14
55
67