1.字典a={“x”:1,“z”:3},b={“y”:2,“z”:4},请设计一个函My_Func(),当My_Func(a,b)时输出c={“x”:1,“y”:2,“z”:3},当My_Func(b,a)时输出c={“x”:1,“y”:2,“z”:4}
a = {"x":1,"z":3}
b = {"y":2,"z":4}
def May_Func(m,n):
m.update(n)
print(m)
May_Func(a,b)
May_Func(b,a)
2.加班薪水问题:联邦法律规定如果员工每周的工作超过了40小时,那么多余的工作时间要支付1.5倍的薪水。例如,如果一个人每小时的薪水是$12,他一周工作了60小时,那么这个人的工资应该为:
(4012)+(1.512*(60-40))=$840
请编写一个程序,输入一个人一周的工作时间和每小时的薪水,输出一周的总薪水,格式如下:
def pay_week(num, wage):
if num < 40:
pay = num * wage
return pay
else:
pay = 40 * wage + (num-40) * wage * 1.5
return pay
num1 = float(input("请输入工作时间:"))
wage1 = float(input("请输入每小时薪水:"))
pay1 = pay_week(num1, wage1)
print("Enter hourly wage: %.2f" % wage1)
print("Enter number of hours worked: %d" % num1)
print("Gross pay for week is: %.2f" % pay1)
3.有一个字典列表如下:
portfolio = [
{‘name’: ‘IBM’, ‘shares’: 100, ‘price’: 91.1},
{‘name’: ‘AAPL’, ‘shares’: 50, ‘price’: 543.22},
{‘name’: ‘FB’, ‘shares’: 200, ‘price’: 21.09},
{‘name’: ‘HPQ’, ‘shares’: 35, ‘price’: 31.75},
{‘name’: ‘YHOO’, ‘shares’: 45, ‘price’: 16.35},
{‘name’: ‘ACME’, ‘shares’: 75, ‘price’: 115.65}
]
name代表品牌名,shares代表分享数量,price代表价格
问题:请编写一个程序,输出价格最高的的前三个商品,如:
[{‘name’: ‘AAPL’, ‘price’: 543.22, ‘shares’: 50}, {‘name’: ‘ACME’, ‘price’: 115.65, ‘shares’: 75}, {‘name’: ‘IBM’, ‘price’: 91.1, ‘shares’: 100}]
import heapq
portfolio = [
{'name': 'IBM', 'shares': 100, 'price': 91.1},
{'name': 'AAPL', 'shares': 50, 'price': 543.22},
{'name': 'FB', 'shares': 200, 'price': 21.09},
{'name': 'HPQ', 'shares': 35, 'price': 31.75},
{'name': 'YHOO', 'shares': 45, 'price': 16.35},
{'name': 'ACME', 'shares': 75, 'price': 115.65}
]
cheap = heapq.nsmallest(3,portfolio,key=lambda x:x["shares"])
expensive = heapq.nsmallest(3,portfolio,key=lambda a:a["price"])
print(cheap)
print(expensive)