在我写的下面的代码中,n = 4,所以有五个if语句,所以如果我想增加n,比如10,那么会有很多if.因此我的问题是:如何用更优雅的东西替换所有if语句?
n, p = 4, .5 # number of trials, probability of each trial
s = np.random.binomial(n, p, 100)
# result of flipping a coin 10 times, tested 1000 times.
d = {"0" : 0, "1" : 0, "2" : 0, "3" : 0, "4" : 0 }
for i in s:
if i == 0:
d["0"] += 1
if i == 1:
d["1"] += 1
if i == 2:
d["2"] += 1
if i == 3:
d["3"] …
Run Code Online (Sandbox Code Playgroud)