python之路——从列表中随机取数

来源:互联网 发布:呱呱漫画下载软件 编辑:程序博客网 时间:2024/06/08 02:14

自学python,这是我发表的第一篇python博客

问题一:从列表中随机取数。

列表为[“a”,”j”,”g”,”h”,”k”,”i”,”l”,”f”,”v”,”b”,”2”,”5”,”x”]

版本1

import numpy as npword_list = ["a","j","g","h","k","i","l","f","v","b","2","5","x"]len_list = range(len(word_list))#将列表生成字典,key为元列表的id序号,values为对应序号的字母,先使用zip将列表组合,再用dict将列表化为字典,id在前,字符在后word_dict = dict(zip(word_len,word_list))result = []random_list = np.random.randint(0,13,5)random_list.sort()def random_dictvalue(random_list):    for i in random_list:        result.append(word_dict[i])    return resultrandom_dictvalue(random_list)print (result)