-3

Python が選択するリストを知りたいので、ステートメントをrandom.choice使用してさまざまな結果を得ることができます。if

thelists = [L1, L2, L3, L4, L5, L6]
theplayers = random.choice(thelists)

L1、L2...、変数 theplayers が選択されたリストを知りたいです。

4

3 に答える 3

4

代わりに使用しないのはなぜですか。後でリストを見つけるため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

于 2012-11-07T21:12:37.323 に答える
2

非常に簡単です:

 res = random.choice(my_list)
于 2012-11-07T20:56:17.547 に答える
1

ドキュメントを見てください:

random.choice(seq)

空でないシーケンスからランダムな要素を返します seqseqが空の場合、 が発生しIndexErrorます。

これがあなたseqのリストです。

選択した要素のインデックスを取得するには、次のようにします。

thelists.index(theplayers)
于 2012-11-07T20:59:03.240 に答える