我将如何使用 NumPy 从 Python 中特定位置的数组中删除元素



我需要用heads = 0tails = 1模拟硬币的掷骷。每次生成介于 1 和 0 之间的随机数时,需要在数组中递增和更新正面或反面。 以下是我的代码:

import numpy, random
flips = numpy.array([0,0])
coin = heads = tails = 0
for i in range(10):
  coin = random.randint(0,1)
  if coin == 0:
      heads += 1
(Now at this point, I want to update the second position of the array because that represents heads, how would I do that?  And the same for the first position, with tails).

请帮忙:)

那不行吗?

import numpy, random
flips = numpy.array([0,0])
for i in range(10):
    flips[random.randint(0,1)] += 1

你可以使用pop

数组 = [1,2,3,4]

数组.pop(1)

现在数组是 [1,3,4]

默认情况下,不带任何参数的 pop 会删除最后一项

数组 = [1,2,3,4]array.pop()

现在数组是 [1,2,3]

最新更新