【发布时间】:2015-05-31 15:56:47
【问题描述】:
我有一个这样的文件:
Tree 5
Jaguar 9
Cat 23
Monkey 12
Gorilla 67
是否可以对这些行中的 3 行进行随机二次抽样? 例如:
Jaguar 9
Gorilla 67
Tree 5
或
Monkey 12
Tree 5
Cat 23
等等?
【问题讨论】:
标签: python
我有一个这样的文件:
Tree 5
Jaguar 9
Cat 23
Monkey 12
Gorilla 67
是否可以对这些行中的 3 行进行随机二次抽样? 例如:
Jaguar 9
Gorilla 67
Tree 5
或
Monkey 12
Tree 5
Cat 23
等等?
【问题讨论】:
标签: python
在readlines 上使用random.sample:
import random
random.sample(open('foo.txt', 'r').readlines(), 3)
【讨论】: