我有两组数据,我想彼此相乘,并将结果存储在每个值的数组中。
现在我有这个:
import csv
from mpdaf.obj import Spectrum, WaveCoord
import matplotlib.pyplot as plt
import pandas as pd
from csv import reader
file_path = input("Enter full transmission curve path : ")
with open(file_path, 'rw') as f:
data = list(reader(f, delimiter=","))
wavelength = [i[0] for i in data]
percentage = [float(str(i[1]).replace(',','.')) for i in data]
spectrum = input("Full spectrum path : ")
spe = Spectrum(filename=spectrum, ext=0)
data_flux = spe.data
flux_array = []
for i in percentage:
for j in data_flux:
flux = i*j
flux_array.append(flux)
print(flux_array)
就像这样,先取i
,然后乘以所有j
,再取下一个i
,依此类推...
我想将第一个i
与第一个j
相乘,然后将值存储在数组中,然后将第二个i
与第二个j
相乘,然后存储值等...
答案 0 :(得分:2)
正如错误消息所述:您的索引i
和j
是浮点数,而不是整数。当您编写for i in percentage:
时,i
会占据percentage
列表中的每个值。相反,您可能想遍历一个范围。这是一个说明差异的示例:
percentage = [50.0, 60.0, 70.0]
for i in percentage:
print(i)
# 50.0
# 60.0
# 70.0
for i in range(len(percentage)):
print(i)
# 0
# 1
# 2
要遍历索引列表,您可能要遍历一个范围:
for i in range(len(percentage)):
for j in range(len(data_flux)):
flux = percentage[i]*data_flux[j]
flux_array.append(flux)
这将遍历每个列表的整数,从0开始到列表的最大索引结束。