实验3

 1 import random
 2 
 3 print('用列表存储随机整数: ')
 4 ls = [random.randint(1, 100) for i in range(5)]
 5 print(ls)
 6 print('\n用集合存储随机整数: ')
 7 s1 = {random.randint(1, 100) for i in range(5)}
 8 print(s1)
 9 print('\n用集合存储随机整数: ')
10 s2 = set()
11 while len(s2) < 5:
12     s2.add(random.randint(1, 100))
13 print(s2)

问题一:范围是【1,100】,能取到100

问题二:范围是(1,5),不能

问题三:不是,四:是的,len(list)是5

 1 datas = {'2049777001': ['篮球', '羽毛球', '美食', '漫画'], '2049777002': ['音乐', '旅行'], '2049777003': ['马拉松', '健身', '游戏']
 2     , '2049777004': [], '2049777005': ['足球', '阅读'], '2049777006': ['发呆', '闲逛'],
 3          '2049777007': [], '2049777008': ['书法', '电影'], '2049777009': ['音乐', '阅读', '电影', '漫画'],
 4          '2049777010': ['数学', '推理', '音乐', '旅行']}
 5 x = datas.values()
 6 list2 = []
 7 list1 = dict()
 8 for i in x:
 9     for j in i:
10         list2.append(j)
11 for y in list2:
12     if y in list1:
13         list1[y] += 1
14     else:
15         list1[y] = 1
16 H = list(list1.items())
17 H.sort(key=lambda x: x[1], reverse=True)
18 for k in H:
19     print(k)

 1 temperatures = [['2022-04-18', '星期一', '10', '22'],
 2                 ['2022-04-19', '星期二', '10', '22'],
 3                 ['2022-04-20', '星期三', '12', '25'],
 4                 ['2022-04-21', '星期四', '13', '28'], ['2022-04-22', '星期五', '16', '31'], ['2022-04-23', '星期六', '19', '31'],
 5                 ['2022-04-24', '星期日', '19', '27']]
 6 list1 = []
 7 list2 = []
 8 for i in temperatures:
 9     x = i[2]
10     y = i[3]
11     list1.append(x)
12     list2.append(y)
13 print("未来一周最低温度:{}摄氏度".format(min(list1)))
14 print("未来一周最高温度:{}摄氏度".format(max(list2)))
15 sum1 = 0
16 sum2 = 0
17 for j in range(0, 7):
18     sum1 += eval(list1[j])
19     sum2 += eval(list2[j])
20 m = sum1 // 7
21 n = sum2 // 7
22 print("未来一周平均气温区间:", m, '~', n, '摄氏度')

 1 import random
 2 
 3 y = random.randint(1, 31)
 4 t = 0
 5 while t < 3:
 6     x = eval(input("说出你认为的五月luck day:"))
 7     t += 1
 8     if x == y:
 9         break
10     elif x < y:
11         print("你猜的日期小了")
12     elif x > y:
13         print("你猜的日期大了")
14 else:
15     print(y)

 1 x,y=input('输入十进制整数x和要转换的进制n(2或8或16)').split(' ')
 2 x=int(x)
 3 y=int(y)
 4 list1=[]
 5 while x!=0:
 6     a=x%y
 7     if a in range(10,17):
 8         list2=['A','B','C','D','E','F']
 9         list3=['10','11','12','13','14','15']
10         a=str(a)
11         b=list3.index(a)
12         a=a.replace(list3[b],list2[b])
13     x=x//y
14     list1.append(a)
15 list1=list1[::-1]
16 for i in range(len(list1)):
17     print(list1[i],end='')

总结:还有很多不熟练,要多加练习。

 

posted @ 2022-04-20 18:17  气工吴旭  阅读(10)  评论(3编辑  收藏  举报