【基础入门题】2021.12.13
生成一个列表(或数组),由10个大于0小于100的随机整数组成。
编程语言:包括但不限于Python
题目来源:派森特给站每日刷题频道
方法一:random.randint()
import random rndList = [] for _ in range(10): rndList.append(random.randint(1,99)) print(', '.join(map(str, rndList)))
rndList = [__import__('random').randint(1,99) for _ in range(10)]
方法二:random.choice
import random rndList = [] for _ in range(10): rndList.append(random.choice(range(1,100))) print(', '.join(map(str, rndList)))
列表推导式:
rndList = [__import__('random').choice(range(1,100)) for _ in range(10)]
方法三:random.sample
1. import random 2. rndList = random.sample(range(1,100),10) 3. 4. print(', '.join(map(str, rndList)))
方法四:random.shuffle
1. import random 2. rndList = [*range(1,100)] 3. random.shuffle(rndList) 4. 5. print(', '.join(map(str, rndList[:10])))