【5秒でわかる】Pythonで配列の値をランダムで一つだけ取り出す(random.choice)

Python3

プログラム

import random
hoge = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(hoge)

moge = random.choice(hoge)
print(moge)

結果

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
4

解説

random.choice(array)
  1. array:値を取り出したい配列

arrayの中からランダムで値を取り出します。

タイトルとURLをコピーしました

Fatal error: Uncaught JSMinException: Unterminated string literal. in /home/miyayamo/miyayamo.com/public_html/wp-content/plugins/cfiltering/lib/library/jsmin-php/jsmin.php:128 Stack trace: #0 /home/miyayamo/miyayamo.com/public_html/wp-content/plugins/cfiltering/lib/library/jsmin-php/jsmin.php(315): JSMin->action(1) #1 /home/miyayamo/miyayamo.com/public_html/wp-content/plugins/cfiltering/lib/library/jsmin-php/jsmin.php(76): JSMin->min() #2 /home/miyayamo/miyayamo.com/public_html/wp-content/plugins/yasakani-cache/yasakani-cache.php(570): JSMin::minify('<script type="a...') #3 /home/miyayamo/miyayamo.com/public_html/wp-content/plugins/yasakani-cache/yasakani-cache.php(925): Yasakani_setting::html_minify('<!doctype html>...', 1, 1, 1) #4 [internal function]: Yasakani_setting::output_cache('<!doctype html>...', 9) #5 /home/miyayamo/miyayamo.com/public_html/wp-includes/functions.php(4339): ob_end_flush() #6 /home/miyayamo/miyayamo.com/public_html/wp-includes/class-wp-hook.php(286): wp_ob_end_flush_all('') #7 /home/miyayamo/miya in /home/miyayamo/miyayamo.com/public_html/wp-content/plugins/cfiltering/lib/library/jsmin-php/jsmin.php on line 128