简体   繁体   English

Python - 如何重新排列数组中的数据?

[英]Python - How to rearrange datas in array?

I work with this kind of array, they are straight from a spectrometer (wavelength, intensity) and I had to treat them with python.我使用这种阵列,它们直接来自光谱仪(波长,强度),我不得不用 python 处理它们。

280.0,1.0
280.4031007751938,1.0
280.8062015503876,1.0
281.2093023255814,1.0
281.61240310077517,1.0
282.015503875969,1.0
282.4186046511628,1.0
282.8217054263566,1.0
283.2248062015504,1.0
283.6279069767442,1.0
284.031007751938,1.0
284.4341085271318,1.0
284.83720930232556,1.0
285.2403100775194,1.0
285.6434108527132,1.0
286.04651162790697,1.0
286.4496124031008,1.0
286.85271317829455,1.0
287.25581395348837,1.0
287.6589147286822,1.0
288.06201550387595,1.0
288.4651162790698,1.0
288.86821705426354,1.0
289.27131782945736,1.0
289.6744186046512,1.0909090909090908
290.07751937984494,1.1818181818181819

My problem is that the function which uses these datas in my python program wants an array where wavelength data is separated from intensity by a : .我的问题是在我的 python 程序中使用这些数据的 function 需要一个阵列,其中波长数据与强度通过:分开。 This form 280: 0.1, .这种形式280: 0.1, . So I am looking for a way to replace the , by a : and put a , to separate from other datas.所以我正在寻找一种方法将,替换为:并放入,以与其他数据分开。 I do not know how to do that, is NumPy able to do that?我不知道该怎么做,NumPy 能做到吗?

I also look for a way to extract some specific values (with a definable step) from the starting data array.我还寻找一种从起始数据数组中提取一些特定值(具有可定义的步骤)的方法。

I also want to remove all decimals for the wavelength as you can see in the below expected example but I think I will be able to deal with this part.正如您在下面的预期示例中所见,我还想删除波长的所有小数,但我认为我将能够处理这部分。

The idea is to have, at the end, such a array:这个想法是在最后拥有这样一个数组:

data_sample = {380: 11.0,
           385: 11.0,
           390: 11.0,
           395: 11.545454545454545,
           400: 12.363636363636363,
           405: 13.454545454545455,
           410: 14.0,
           415: 14.181818181818182,
           420: 15.363636363636363,}

I hope I succeed to well describe what I am trying to do and you can help me or guide me towards answers.我希望我能成功地描述我正在尝试做的事情,你可以帮助我或指导我找到答案。

Thank you very much:)非常感谢:)

As I understand the colour.SpectralDistribution method you need to pass a dictionary as argument where wavelength is the key and intensity the value.据我了解colour.SpectralDistribution方法您需要将字典作为参数传递,其中波长是关键,强度是值。

Basically, for each line you read in your input file you have to:基本上,对于您在输入文件中读取的每一行,您必须:

  • split it based on the comma separator to retrieve the wavelength at the leftend and the intensity at the rightend根据逗号分隔符将其拆分以检索左端的波长和右端的强度
  • convert values from string to float + round the wavelength value将值从字符串转换为浮点数 + 舍入波长
  • push the wavelength and intensity as key/value pair into a dictionary.波长强度作为键/值对推送到字典中。

You can achieve this goal like so:你可以像这样实现这个目标:

with open("raw_data.txt", "r") as raw_data_file:
  lines = [line.rstrip("\n").split(",") for line in raw_data_file]
  
spectral_distribution_input = {
  round(float(wavelength)): float(intensity) for (wavelength, intensity) in lines
}

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

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