import random from random import randint list_one=["name","age",18] choice_num=random.choice(list_one) print(choice_num) index_num=randint(1,1000)%len(list_one) print(list_one[index_num]) ‘‘‘ 利用下表随机取值 ‘‘‘ def getsomeone(listobject): if isinstance(listobject, list) : index_num=randint(1,1000)%len(listobject) return listobject[index_num] else: return None ‘‘‘ 利用random中的choice函数随机取值 ‘‘‘ def getsomeone1(listobject): if isinstance(listobject, list): someone=random.choice(listobject) return someone else: return None print(getsomeone(list_one)) print(getsomeone1(list_one))
原文:https://www.cnblogs.com/tallme/p/11173422.html