Nice to meet you, this is cells_comp. I, who was a ROM specialist, started Qiita at midnight. I'm sorry that the first article to commemorate is such a bad article, but I would like to post what I came up with for the time being.
If you want to randomly sort the elements of list (l) in Python, think straightforwardly
--Sort by random.shuffle (l) --Sort by l2 = random.sample (l, len (l)) --Correctly, "random acquisition of the specified number from the list without duplication" is performed. --The specified number = all elements, so the result is a shuffled list.
I think that will come out. By the way, which of these two methods is faster? You may hear a voice saying ** Use numpy if you want to say speed **, but please forgive me.
Python 3.5.5 Intel® Core™ i7-6700 3.4GHz Windows10 (WSL) Ubuntu 16.04 LTS
The target list contains integers.
import random
import time
if __name__ == '__main__':
l = [i for i in range(1000000)]
start = time.time()
random.shuffle(l)
elapsed_time = time.time() - start
print("elapsed_time:" + str(format(elapsed_time)) + "s")
result
elapsed_time:0.6656289100646973s
import random
import time
if __name__ == '__main__':
l = [i for i in range(1000000)]
start = time.time()
l2 = random.sample(l,len(l))
elapsed_time = time.time() - start
print("elapsed_time:" + str(format(elapsed_time)) + "s")
result
elapsed_time:0.7895145416259766s
Yes, random.shuffle () is about 20% faster. After all, it is not optimal because it is originally a function for random extraction.
import random
import time
import numpy
if __name__ == '__main__':
l = [i for i in range(1000000)]
start = time.time()
numpy.random.shuffle(l)
elapsed_time = time.time() - start
print("elapsed_time:" + str(format(elapsed_time)) + "s")
result
elapsed_time:0.1087648868560791s
***the end! Closed! that's all! Everyone disbanded! *** ***
Yes, this is my first post. I was so scared that I didn't post it until now, but when I try it, it's easy. It's a self-satisfying post, but I hope it helps even one person. I would like to continue posting in a timely manner. Well then, thank you for your cooperation.
Recommended Posts