简体   繁体   English

numpy:通过随机不同选择索引来更改数组中的值

[英]numpy:Change values in array by randomly differently selecting index

I am new in numpy, and I am having troubles with simple managment of numpy arrays.我是 numpy 的新手,我在简单管理 numpy arrays 时遇到了麻烦。

I am doing a task in which it said that randomly daily select different 12 items in a numpy by index to change its value.我正在执行一项任务,它说每天随机 select 不同的 12 个项目在 numpy 中按索引更改其值。

import numpy as np
import random
N = 20
s = np.zeros([N])
for t in range(12):
    randomindex = random.randint(0,len(s)-1)
    s[randomindex] = 10

thanks for u answering.I'm sorry for my describing,i'm not good at how writting problem of python by english.--..I will give more detailed information谢谢你的回答。我很抱歉我的描述,我不擅长用英文写 python 的问题。--..我会提供更详细的信息

e.g. s=(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20) 

and i randomly choose a item from its numpy by its index,我从它的索引中随机选择一个项目 numpy,

randomindex=random.randint(0,len(s)-1), randomindex=random.randint(0,len(s)-1),

randomindex will be 0-19, randomindex 将是 0-19,

and s(randomindex)=10,if the randomindex is 2 means s(2) is 10, s=(1,2,10,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20).并且s(randomindex)=10,如果randomindex为2表示s(2)为10,s=(1,2,10,5,6,7,8,9,10,11,12,13,14, 15、16、17、18、19、20)。

And if i want choose 3 items, i do 3 times'for',how can i everytimes choose different index changes its value in numpy.如果我想选择 3 个项目,我做了 3 次'for',我怎么能每次选择不同的索引改变它在 numpy 中的值。 and daily which means i will give sum the new s and given to a new numpy R[T] like:并且每天这意味着我将把新的 s 和给一个新的 numpy R[T] 之和,例如:

import numpy as np
import random
N = 20
s = np.zeros([N])
T=10 #DAY
R = np.zereos([T])
for t in range(T-1):
    R[T+1]=R[T]+R[T]*3
    for t in range(12):
        randomindex = random.randint(0,len(s)-1)
        s[randomindex] = 10
        R[T]=np.sum(s)


I'm having a little difficulty understanding what you're asking, but I think you want a way to be selecting different values in a randomized order.我有点难以理解你在问什么,但我认为你想要一种以随机顺序选择不同值的方法。 And the problem with the above code is that you may be getting duplicates.上面代码的问题是你可能会得到重复。

I have two solutions.我有两个解决方案。 One, you can use the Python random library .一、可以使用Python随机库 The Python random.shuffle function will randomly shuffle all the values in an iterable (such as a list or array). Python random.shuffle function 将随机打乱可迭代(例如列表或数组)中的所有值。 You can then proceed to access them sequentially as you normally would.然后,您可以像往常一样按顺序访问它们。 Here's an example of Random Shuffle.这是随机随机播放的示例。

list1 = ["Apple", "Grapes", "Bananas","Grapes"]
random.shuffle(list1)
print(list1)

The second solution doesn't involve the use of a library.第二种解决方案不涉及使用库。 You can simply make an addition into the code above.您可以简单地添加到上面的代码中。 Create a new empty list, and every time you retrieve a value add it into this list.创建一个新的空列表,每次检索值时将其添加到此列表中。 Also, whenever you retrieve a value check to see whether it's already located in the list.此外,每当您检索值时,请检查它是否已经位于列表中。 For instance, if you retrieve the value 5, add it to the list.例如,如果您检索值 5,请将其添加到列表中。 If later on, you end up with the value 5 again, run a check through the list and you will find that 5 already exists in it.如果稍后,您再次得到值 5,请检查列表,您会发现 5 已经存在于其中。 You can then reset that iteration and take input again.然后,您可以重置该迭代并再次输入。

You can append to a list with the following code.您可以使用以下代码将 append 到一个列表中。

newlist = ["Python", "Java","HTML","CSS"]
newlist.append("Ruby")
print(newlist)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM