문제

I'd like to create a million unordered list, xrange(1, 1000000) for eg gives me an ordered list. I think I need something like xrange but generates it in an unordered fashion. I think I can generate a list manually by looping around random.randint and some manual checks to guarantee uniqueness of number in list but I reckon it would be time consuming. Any ideas?

도움이 되었습니까?

해결책

import random
L = range(1, 1000000)
random.shuffle(L)  # shuffles in-place

on Python3, you will need to use

L = list(range(1, 1000000))
random.shuffle(L)  # shuffles in-place

다른 팁

Use the random module from NumPy. Specifically np.random.permutation

>>> import numpy as np
>>> np.random.permutation(5)
array([2, 1, 0, 3, 4])

If you're using numpy you can use permutation:

numpy.random.permutation(10)
>> array([1, 7, 6, 0, 5, 9, 2, 3, 8, 4])
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top