常见用法:
- 在for循环中遍历每个元素。
- if语句中判断某个元素是否在列表中。
示例代码:
#遍历数组winning_doors中的元素
for winning_door in winning_doors:
choices = [1,2]
if first_try in choices:
choices.remove(first_try)
完整示例代码:
import numpy as np
import numpy.random as random
random.seed(42)
test_num = 10000
winning_doors = random.randint(0, 3, test_num)
changed_mind_num = 0
insist_num = 0
for winning_door in winning_doors:
first_try = random.randint(0, 3)
remaing_choices = [i for i in range(3) if i != first_try]
wrong_choices = [i for i in range(3) if i != winning_door]
if first_try in wrong_choices: #如果first_try在wrong_choices列表中
wrong_choices.remove(first_try)
screened_out = random.choice(wrong_choices)
remaing_choices.remove(screened_out)
changed_mind_try = remaing_choices[0]
changed_mind_num += 1 if