gpt4 book ai didi

python - 创建列表列表的列表以对文本文件中的数据进行排序

转载 作者:行者123 更新时间:2023-12-01 00:49:27 25 4
gpt4 key购买 nike

我正在尝试读取和分析从分子动力学模拟返回的数据,它看起来像这样,但大约有 50000 行:

40 443.217134221125 -1167.16960983145 -930.540717277902 -945.149746592058 14.6090293141563 -76510.1177229871 4955.17798368798 17.0485096390963 17.0485096390963 17.0485096390963
80 659.39103652059 -923.638916369481 -963.088128935875 -984.822539088925 21.7344101530497 14390.2520385682 4392.18167603894 16.3767140226773 16.3767140226773 16.3767140226773
120 410.282687399253 -979.413482414461 -978.270613122515 -991.794079036891 13.5234659143754 -416.30808174241 4398.37322990079 16.3844056974088 16.3844056974088 16.3844056974088

第二列代表温度。我希望将文件的全部内容放在一个列表中,其中包含根据温度划分每一行的列表。例如,主列表中的第一个列表将包含温度为 50+/-25K 的每一行,主列表中的第二个列表将包含温度为 100+/-25K 的每一行,第三个列表为 150 +/-25K等
这是我到目前为止的代码:

for nbligne in tqdm(range(0,len(LogFullText),1), unit=" lignes", disable=False):
string = LogFullText[nbligne]
line = string.replace('\n','')
Values = line.split(' ')

divider = float(Values[1])
number = int(round(divider/ecart,0))
if number>0 and number < (nbpts+1):
numericValues = []
for nbresultat in range(0,len(Values)-1,1):
numericValues = numericValues + [float(Values[nbresultat+1])]

TotalResultats[number-1].append(numericValues)

包含数据的整个文档存储在列表 LogFullText 中,其中我删除末尾的\n 并使用 line.split(' ') 分割数据,然后我知道主文件的哪个“部分” list,TotalResultats,数据行必须使用变量号存储,在我的示例中,ecart 的值为 50。

从我在空闲状态下的测试来看,这应该可行,但实际上会发生什么情况,列表 numericValues 被附加到 TotalResultats 的每个部分,这使得整个“排序”过程毫无意义,因为我最终只是得到了 nbpts 乘以相同的列表。

编辑:所需的输出例如是让 TotalResultats[0] 仅包含这些行:

440 49.9911561170447 -1002.727121613 -1002.72088094757 -1004.36865629012 1.64777534254374 -2.30045369926927 4346.38067015602 16.319590369315 16.319590369315 16.319590369315
480 42.0678318129411 -1002.69068695093 -1003.09270361295 -1004.47931559314 1.38661198019398 148.219667654185 4345.58826561836 16.3185985476593 16.3185985476593 16.3185985476593
520 43.0855216044083 -1003.4761833678 -1003.33820025832 -1004.75835665467 1.42015639634654 -50.877194096845 4345.23364199522 16.3181546401367 16.3181546401367 16.3181546401367

而 TotalResults[1] 将包含以下内容:

29480 109.504432929553 -980.560226069922 -998.958927113452 -1002.5683396275 3.6094125140473 6797.60091557441 4336.52501942717 16.3072458525354 16.3072458525354 16.3072458525354
29520 106.663291994583 -987.853629557979 -998.63436605413 -1002.15013076443 3.51576471029626 3975.43407740646 4344.84444478408 16.3176674266037 16.3176674266037 16.3176674266037
29560 112.712019757891 -1020.65735849343 -998.342638324154 -1002.05777718853 3.71513886437272 -8172.25412368794 4374.81748831773 16.3551041162317 16.3551041162317 16.3551041162317

TotalResults[2] 将是:

52480 142.86322849701 -983.254970494784 -995.977110177167 -1000.68607319299 4.70896301582636 4687.60299340191 4348.30194824999 16.321994657312 16.321994657312 16.321994657312
52520 159.953459288754 -984.221801201968 -995.711657311665 -1000.9839371836 5.27227987193358 4233.04866428826 4348.82254074761 16.3226460049712 16.3226460049712 16.3226460049712
52560 161.624843851124 -1011.76969126636 -995.320907086768 -1000.64827802848 5.32737094170867 -6023.57133443538 4375.12133631739 16.3554827492176 16.3554827492176 16.3554827492176

在第一种情况下,

TotalResultats[0][0] = [49.9911561170447, -1002.727121613, -1002.72088094757, -1004.36865629012, 1.64777534254374, -2.30045369926927, 4346.38067015602, 16.319590369315, 16.319590369315, 16.319590369315]

如果有帮助,我正在 Visual Studio 中使用 python 3.6.8 进行编码

非常感谢!

最佳答案

我推荐使用pandas。它是一个非常强大的工具,可以在 python 中处理表格数据。就像python里面的excel或者sql一样。假设 1.csv 包含您在问题中提供的数据。然后您可以轻松加载数据、过滤数据并保存结果:

import pandas as pd

# load data from file into pandas dataframe
df = pd.read_csv('1.csv', header=None, delimiter=' ')

# filter by temperature, column named 0 since there is no header in the file
df2 = df[df[0].between(450, 550)]

# save filtered rows in the same format
df2.to_csv('2.csv', header=None, index=False, sep=' ')

Pandas 可能比普通的 Python 语法更难学习,但这是非常值得的。

关于python - 创建列表列表的列表以对文本文件中的数据进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56687573/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com