Python random.sample()

时间:2017-04-14 17:20:23

标签: python-3.x

Attribute Error:built-in function or method object has no attribute sample.

此代码生成

const dateTime = '2017-04-17 18:26:03';
const parts = dateTime.split(/[- :]/);
const wanted = `${parts[2]}/${parts[1]}/${parts[0]} ${parts[3]}:${parts[4]}`;
console.log(wanted);

我是Python初学者。请帮忙。

1 个答案:

答案 0 :(得分:2)

您正在从random包导入random功能。您需要导入样本函数。

from random import sample
sample([1, 20, 3], 2)

或导入random包并显式调用样本函数,如

import random
random.sample([1, 20, 3], 2)