随机列表问题
随机生成一个长度为100的整数列表,其元素范围为1~100,将该列表以每10个一行(元素之间以空格分隔)写入一个文本文件("record.txt") ,将文本文件("record.txt")中的数字读入一个列表,并按数字的升序输出该列表。
注:enumerate() 取出的两个值,一个为数的索引,一个为数的值
from random import randint lis = [ ] for i in range(100): lis.append(randint(1,100)) with open("record.txt", " ① ") as f: str = "" for i, v in enumerate(lis): str = str + "{} ".format(v) if ② == 0: b = ③ (str + "\n") str = "" lis_date = [ ] with open("record.txt") as f: for line in ④ : line = line.strip() data = line.split() for v in data: lis_date.append(eval(v)) last_date = ⑤ (lis_date) print(last_date)