实验三
作者:互联网
import random print('用列表存储随机整数: ') ls = [random.randint(1, 100) for i in range(5)] print(ls) print('\n用集合存储随机整数: ') s1 = {random.randint(1, 100) for i in range(5)} print(s1) print('\n用集合存储随机整数: ') s2 = set() while (len(s2) < 5): s2.add(random.randint(1, 100)) print(s2)
1.从1到100,能取到100
2.0到4,不包括5;1到4,不包括5
3.不一定是5,因为集合没有重复的元素,但是连续取五次随机数有可能取到重复的随机数,然后被集合去重
4.一定是五,因为while循环保证了如果集合元素不够5会补充到5
datas = {'2049777001': ['篮球', '羽毛球', '美食', '漫画'], '2049777002': ['音乐', '旅行'], '2049777003': ['马拉松', '健身', '游戏'], '2049777004': [], '2049777005': ['足球', '阅读'], '2049777006': ['发呆', '闲逛'], '2049777007': [], '2049777008': ['书法', '电影'], '2049777009': ['音乐', '阅读', '电影', '漫画'], '2049777010': ['数学', '推理', '音乐', '旅行']} bbb = [id2 for id1 in list(datas.values()) for id2 in id1] ddd = [(id3, bbb.count(id3)) for id3 in list(set(bbb))] eee = sorted(ddd, key=lambda x: x[1], reverse=1) for i in range(len(eee)): print(eee[i][0] + f":{eee[i][1]}")
temperatures= [['2022-04-18', '星期一', '10', '22'], ['2022-04-19', '星期二', '10', '22'], ['2022-04-20', '星期三', '12', '25'], ['2022-04-21', '星期四', '13', '28'], ['2022-04-22', '星期五', '16', '31'], ['2022-04-23', '星期六', '19', '31'], ['2022-04-24', '星期日', '19', '27']] low=[int(id1[2]) for id1 in temperatures] high=[int(id2[3]) for id2 in temperatures] print(f"未来一周最低气温:{min(low)}°C") print(f"未来一周最高气温:{max(high)}°C") print(f"未来一周平均气温区间:{int(sum(low)/len(low))}°C ~ {int(sum(high)/len(high))}°C")
from random import * def cai(x): if x > aaa: return "猜晚啦,你的lucky day已经过啦\n再猜(1~31):" elif x < aaa: return "猜早啦,你的lucky day还没到呢\n再猜(1~31):" else: return "哇,猜中了:-)" aaa, i = randint(1, 31), 0 bbb = int(input("猜猜2022年5月那一天会是你的lucky day:-)\n你有三次机会,猜吧(1~31):")) while bbb != "哇,猜中了:-)" and i < 2: bbb, i = int(input(cai(bbb))), i + 1 if i == 2: print(f"次数用光了\n偷偷告诉你,5月你的lucky day是{aaa}号,good luck:—)")
x, old_code = eval(input("输入需要转换的数字:")), [] n, s = eval(input("输入需要转换的进制:")), x while x // n != 0: old_code.append(x % n) x = x // n if x // n == 0: old_code.append(x % n) new_code = list(str(i) if i < 10 else chr(i + 55) for i in old_code[::-1]) print(f"将{s}转换成{n}进制之后是:" + "".join(new_code))
标签:code,04,31,bbb,实验,2022,print 来源: https://www.cnblogs.com/shadow824/p/16188677.html