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

时间:2016-03-10 20:44:03

标签: pythonxy

我需要使用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).

请帮助:)

2 个答案:

答案 0 :(得分:0)

你可以使用pop

array = [1,2,3,4]

array.pop(1)

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

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

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

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

答案 1 :(得分:0)

不会有这个伎俩吗?

import numpy, random

flips = numpy.array([0,0])

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