Numpy: how to select items in numpy and assign its value(Numpy:如何在 numpy 中选择项目并为其赋值)
问题描述
我在按列表分配新值时遇到问题.
I have got a problem in assigning new value by list.
我想通过 numpy 数组的索引更改 s numpy 中的 12 个项目值,我希望我选择的每个索引都是不同的.所以我制作了一个列表 random.sample(range(0,len(s),12) 来选择 12 个不同的索引.通过这个索引更改 numpy 数组 s() 中的一些值但是,我收到错误:SyntaxError: can't assign to function call
I want to change 12 items values in s numpy by numpy array's index ,and i hope every index i choose is different.
so i made a list random.sample(range(0,len(s),12) to select 12 different index.And through this index change some of values in numpy array s()
However, I'm getting the error: SyntaxError: can't assign to function call
import numpy as np
import random
N = 20
s = np.zeros([N])
alist = random.sample(range(0,20),12)
alist
for i in alist:
s(i)=10
推荐答案
我不完全确定你想在这里实现什么,但 s(i) 是你的问题:圆括号暗示一个函数调用,但 s 是一个 numpy 数组,所以这不起作用.我认为您正在尝试为列表编制索引,在这种情况下您将使用 s[i].
I'm not totally sure what you're trying to achieve here, but s(i) is your problem: round brackets imply a function call, but s is a numpy array, so this won't work . I think you're trying to index the list, in which case you'd use s[i].
这篇关于Numpy:如何在 numpy 中选择项目并为其赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Numpy:如何在 numpy 中选择项目并为其赋值
基础教程推荐
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01
- Kivy 使用 opencv.调整图像大小 2022-01-01
