This is one way of doing it:
import random
def coinflipping():
#generating 100 coin flips
coins = []
coin_sides = ['H', 'T']
for i in range(100):
coins.append(coin_sides[random.randint(0, 1)])
return coins
sides = coinflipping()
print(sides)
for i,t in enumerate(sides):
if t == 'H':
print(i)
Remember that index
returns the first occurrence of an item. And since sides
are your static list it always returns 1
because H
is first met in the index 1
in your random
case.
You can check enumerate here.
Another way of doing it is this:
a = [np.random.choice(['H','T']) for x in range(100)]
result = [x[0] for x in enumerate(a) if x[1]=='H']
# returns a list of indexes where 'H' was flipped.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…