(Python) TypeError:只有整数标量数组可以转换为标量索引 [英] (Python) TypeError: only integer scalar arrays can be converted to a scalar index

查看:248
本文介绍了(Python) TypeError:只有整数标量数组可以转换为标量索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我编译以下程序时,Python 抛出 TypeError: only integer scalar arrays can be convert to a scalar index.

When I compile the following program, Python throws a TypeError: only integer scalar arrays can be converted to a scalar index.

这似乎是一个相对简单的程序执行,但我似乎无法解决它.

This seems like a relatively straightforward program execution, but I can't seem to resolve it.

频率

import matplotlib.pyplot as plt
import numpy as np

def period(n):
    #masses
    m = [1] * n
    #lengths
    l = [2] * n
    M = sum(m)
    num = 2 * math.pi * n

for i in range(n):
    dem = dem + math.sqrt(g * m[i]/(l[i] * M)) 
return num/dem   

x = np.arange(1, 10,1)
y = period(x)
plt.plot(x,y)
plt.show()

M == 从质量 m_j 的 j==1 到 n 的总和.我希望程序简单地显示 period 的图,其中 period(n) 简单地由 1n 的总和定义sqrt(g * m_j/(l_j * M)).

Let M == sum from j==1 to n of the masses m_j. I expect the program to simply display a plot of period where period(n) is simply defined by the sum from 1 to n of sqrt(g * m_j/(l_j * M)).

推荐答案

使用列表推导将 period 函数应用到 x 数组中的每个条目,就像这样 -

Use list comprehension to apply period function to each entry in the x array like this -

y = np.array([period(i) for i in x])

另外,你需要同时初始化 demg -

Also, you need to initialize both dem and g -

dem = 0.0
g = 9.8

这篇关于(Python) TypeError:只有整数标量数组可以转换为标量索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆