Python が選択するリストを知りたいので、ステートメントをrandom.choice
使用してさまざまな結果を得ることができます。if
thelists = [L1, L2, L3, L4, L5, L6]
theplayers = random.choice(thelists)
L1、L2...、変数 theplayers が選択されたリストを知りたいです。
代わりに使用しないのはなぜですか。後でリストを見つけるためrandom.randint
に使用する必要がありません。list.index
from random import randint
# your list of lists
l = [[1,2,3],[4,5,6],[7,8,9]]
# choose a valid *index* into l, at random
index = randint(0,len(l) - 1)
# use the randomly chosen index to get a reference to the list
choice = l[index]
# write your conditionals which handle different choices
if index == 1:
print 'first list'
elif index == 2:
print 'second list'
...
これは、選択するたびにrandom.choice
使用するよりも効率的です。list.index
非常に簡単です:
res = random.choice(my_list)
ドキュメントを見てください:
random.choice(seq)
空でないシーケンスからランダムな要素を返します
seq
。seq
が空の場合、 が発生しIndexError
ます。
これがあなたseq
のリストです。
選択した要素のインデックスを取得するには、次のようにします。
thelists.index(theplayers)