繁体   English   中英

从文本文件Python收集5个随机样本

[英]Gather a random sample of 5 from text file Python

我想从包含100,000个的列表中抓取5个psuedorandom项。 这些列表项的长度为25个字符,目前我的脚本仅获得第一个字符(它们似乎不是来自列表)。

我的密码

import random

with open("output.txt") as hashes:
    sample_five = random.sample(hashes, 5)

    print sample_five

更新的抛出错误: object of type 'file' has no len()sample_five = random.sample(hashes, 5)

output.txt列出

['KP9UF8GMKH0VNA1TURAAG6IHO','EQNUB4W1BYM7B5R6IWNWGEV4&','6QQSUUMCW9XPFVMQZUJKMAMW0','AZFUE8GWKHEVNO1TUEAADIHS','0DLWPEOD8345QASDZXCVBNHSD']

print sample_five我想输出列表,如上所示,但其输出为:

>>> [34378, 41139, 76739, 27686, 23880]

谁能看到它为什么不从列表中抢取(5)个随机列表项的原因? 谢谢

import random
import ast
with open("output.txt") as hashes:
    input = ast.literal_eval(hashes.read())
    sample_five = random.sample(input, 5)

    print sample_five

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM