如何使用 NumPy 从 Python 中特定位置的数组中删除元素?
How would I remove an element from an array at a specific position in Python, using NumPy?
我需要用heads = 0
和tails = 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).
请帮忙:)
你可以使用 pop
数组 = [1,2,3,4]
array.pop(1)
现在数组是 [1,3,4]
默认情况下,不带任何参数的 pop 会删除最后一项
数组 = [1,2,3,4]
array.pop()
现在数组是 [1,2,3]
这样不行吗?
import numpy, random
flips = numpy.array([0,0])
for i in range(10):
flips[random.randint(0,1)] += 1
我需要用heads = 0
和tails = 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).
请帮忙:)
你可以使用 pop
数组 = [1,2,3,4]
array.pop(1)
现在数组是 [1,3,4]
默认情况下,不带任何参数的 pop 会删除最后一项
数组 = [1,2,3,4] array.pop()
现在数组是 [1,2,3]
这样不行吗?
import numpy, random
flips = numpy.array([0,0])
for i in range(10):
flips[random.randint(0,1)] += 1