简体   繁体   English

将循环中的输出分配给单个列表或数组

[英]assign the outputs in a loop to a single list or array

I am still finding my way in Python so this question may be very basic: I am trying to generate some cosine waves in python and then do a Fourier transform on them. 我仍然在用Python寻找自己的方式,所以这个问题可能是非常基本的:我试图在python中生成一些余弦波,然后对其进行傅立叶变换。 I am wrote the following code: 我写了以下代码:

from pylab import *
from numpy import *
x = linspace(-5,5,100)
t = linspace(0,2000,2001)
w1=cos(2*pi*3*t/2001)
w2=cos(2*pi*10*t/2001)
fourier=0
long(fourier)

Trace = w1+w2
for i in range(1,125,1):
    fourier[i] = sum(cos(2*pi*i*t/2001)*Trace)

print fourier
f=linspace(1,125,125)
plot(f,fourier)
show()

so instead of calculating the values frequency by frequency I want to make a loop that takes each frequency from 1 to 125 and store the outputs in one list so that I can plot them. 因此,我不想做成一个频率一个频率的值,而是想做一个循环,使每个频率从1到125,并将输出存储在一个列表中,以便我可以绘制它们。

Python does not like this and gives a message saying: Python不喜欢这样,并给出一条消息:

fourier[i] = sum(cos(2*pi*i*t/2001)*Trace)
TypeError: 'int' object does not support item assignment

Any idea why? 知道为什么吗?

may be you want: 可能是您想要的:

fourier = []

you decleared it as integer, you need to declear as list. 您将其清除为整数,则需要将其清除为列表。

your fourior type is integer and you need to make it as list so it can support index assignment 您的Fourior类型是整数,您需要将其作为列表,以便它可以支持索引分配

also remove long(fourier) from your code 还从您的代码中删除long(fourier)

for i in range(1,126):
    fourier[i] = sum(cos(2*pi*i*t/2001)*Trace)

this will give you index out of index error 这将使您的索引超出索引错误

correct would be: 正确的是:

for i in range(1,126):    # you dont need last option as 1, because range step is one by default
    fourier.append(sum(cos(2*pi*i*t/2001)*Trace))
 OR
    fourier.insert(i,sum(cos(2*pi*i*t/2001)*Trace))

its better to use list comprehension and you need to set range to 126 to match the dimensions of f for plotting: 最好使用列表推导,您需要将范围设置为126以匹配f的尺寸进行绘图:

fourier = [sum(cos(2*pi*i*t/2001)*Trace) for i in range(1,126)]

You need an array initialization: fourier=[] 您需要数组初始化: fourier=[]

for i in range(1,125):
  fourier.append(sum(cos(2*pi*i*t/2001)*Trace))

There are several ways to build up a list of objects/numbers. 有几种方法可以建立对象/数字列表。

The simplest is a for loop: 最简单的是for循环:

# create a list of squared numbers
squares = []
for item in range(10):
    squares.append(item*item)

This can also be done using a "list comprehension": 这也可以使用“列表理解”来完成:

# produces identical list to the above for loop
squares = [(item * item) for item in range(10)]

Finally, you are using numpy, this allows to do "vector" operations on arrays. 最后,您正在使用numpy,这允许对数组执行“向量”操作。 The following is a simple example of the above square code. 以下是上述平方码的简单示例。

eg. 例如。

numbers = numpy.array(range(10))
# or you could write -- numbers = numpy.arange(10)
squares = numbers * numbers

However, you can also do very complicated vector arithmetic as well. 但是,您也可以执行非常复杂的矢量算法。 This allows you to create your Fourier transform very easily. 这使您可以非常轻松地创建傅立叶变换。

indices = array(range(125), ndmin=2).T
arr_fourier =  (cos(2*pi*indices*t/2001)*Trace).sum(axis=1)

Note that i has been replaced by indices . 注意, i已被indices代替。 When multiplying the indices by t we create a transpose of the indices array so that we end up creating a 2d array as a result. 当乘以indicest我们创建的转置indices阵列,使得我们最终创建一个二维数组作为结果。 At the end of the process you sum over the one of the axes (in this case 1) to reduce the 2d array back down to a 1d array of length 125. 在该过程的最后,您对一个轴(在本例中为1)求和,以将2d数组缩小为长度为125的1d数组。

from pylab import *
from numpy import *
x = linspace(-5,5,100)
t = linspace(0,2000,2001)
w1=cos(2*pi*3*t/2001)
w2=cos(2*pi*10*t/2001)
fourier=[]
Trace = w1+w2
    for i in range(1,126):
        fourier.append(sum(cos(2*pi*i*t/2001)*Trace))
print fourier
f=linspace(1,125,125)
plot(f,fourier)
show()

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

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