Numpy:如何在 numpy 中选择项目并为其赋值

2023-11-08Python开发问题
3

本文介绍了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 中选择项目并为其赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

在xarray中按单个维度的多个坐标分组
groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)...
2024-08-22 Python开发问题
15

Pandas中的GROUP BY AND SUM不丢失列
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)...
2024-08-22 Python开发问题
17

GROUP BY+新列+基于条件的前一行抓取值
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)...
2024-08-22 Python开发问题
18

PANDA中的Groupby算法和插值算法
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)...
2024-08-22 Python开发问题
11

PANAS-基于列对行进行分组,并将NaN替换为非空值
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)...
2024-08-22 Python开发问题
10

按10分钟间隔对 pandas 数据帧进行分组
Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)...
2024-08-22 Python开发问题
11