有两个集合,一个表示商店当前包含的水果种类与个数,另一个表示仓库包含的水果种类,从仓库中随机取n次水果(每次取的个数为10以内随机数),n为仓库水果种类数的一半,如果仓库水果数为奇数则向下取整,仓库与商店中水果种类可能有重叠,但仓库与商店中的水果种类均不重复,输出最后商店中水果种类及其对应数量(按数量多少从小到大输出)。
import random
store = {"apple": 1, "banana": 2, "watermelon": 3}
stock = set(["pear", "cherry", "apple", " orange"])
num = int(len(stock) // 2)
print('取'+str(num)+'次水果')
for i in range(0,num):
choice = random.choice(list(stock))
n=random.randint(1,10)
store[choice]=n
store_list=sorted(store.items(),key=lambda x:x[1])
print(store_list)