Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
换句话说,如果我调用 getList(10,6) 那么它将是一个长度为 10 的列表,其数字范围为 1 到 6,允许重复。例如 [1,4,2,6,3,8,5,5,2,1]
我只是重复调用randrage并附加吗?有没有更好的办法?
您可以使用列表推导:
import random def getList(L, N): return [random.randint(1,N) for _ in xrange(L)]