【发布时间】:2014-11-09 14:18:22
【问题描述】:
我正在尝试通过将每一列放入它自己的数组然后对其进行操作来重新排列一些数据文件。稍后我将使用列数组的索引对行进行重新排序,但是现在,我对循环以及如何使索引起作用感到很困惑。
我目前的代码如下:
import csv as csv
import sys as sys
freq = []
J_i = []
w_i = []
n_i = []
J_f = []
w_f = []
n_f = []
infile = open('Rearrange Column Test.txt')
sys.stdout = open('Rearrange Column TestNEW.txt', 'w')
for line in csv.reader(infile, delimiter='\t'):
newline = [line[i] for i in [20, 0, 3, 4, 7, 9, 10]]
newline[2] = newline[2].split('=')[1]
newline[4] = newline[4].split('=')[1]
freq.append(float(newline[0]))
J_i.append(float(newline[1]))
w_i.append(float(newline[2]))
n_i.append(float(newline[3]))
J_f.append(float(newline[4]))
w_f.append(float(newline[5]))
n_f.append(float(newline[6]))
for j in freq, J_i, w_i, n_i, J_f, w_f, and n_f:
print freq[j], J_i[j], w_i[j], n_i[j], J_f[j], w_f[j], n_f[j]
if J_f[j] == J_i[j]:
if w_i[j] == 0.5 and w_f[j] == 0.5:
Tline = "Q_{+}^{+}("
elif w_i[j] == -0.5 and w_f[j] == 0.5:
Tline = "Q_{-}^{+}("
elif w_i[j] == -0.5 and w_f[j] == -0.5:
Tline = "Q_{-}^{-}("
elif J_f[j] - J_i[j] == 1:
if w_i[j] == 0.5 and w_f[j] == 0.5:
Tline = "R_{+}^{+}("
elif w_i[j] == -0.5 and w_f[j] == 0.5:
Tline = "R_{-}^{+}("
elif w_i[j] == -0.5 and w_f[j] == -0.5:
Tline = "R_{-}^{-}("
elif J_f[j] - J_i[j] == -1:
if w_i[j] == 0.5 and w_f[j] == 0.5:
Tline = "P_{+}^{+}("
elif w_i[j] == -0.5 and w_f[j] == 0.5:
Tline = "P_{-}^{+}("
elif w_i[j] == -0.5 and w_f[j] == -0.5:
Tline = "P_{-}^{-}("
print Tline, J_i[j], ")"
sys.stdout.close()
我只是对我可以使用的索引感到困惑。我想确保从每个列数组中打印完全相同的位置(来自 freq 的第 5 个值和来自 J_i 的第 5 个值等),并且还对 J_i 和 J_f 列数组中的相同索引值进行操作。有人可以帮我让这个循环正常工作吗?
示例数据:
0.5 0.6801 0.5 omi=-0.5 -1 ---> 1.5 0.5 omf= 0.5 -1.0 0.3301 frq= -0.3501 0.6667 0.5974 0 0 1.00 frq= 3723.6699 xint= 1.0667
1.5 0.3301 0.5 omi= 0.5 -1 ---> 0.5 0.5 omf=-0.5 -1.0 0.6801 frq= 0.3501 0.6667 0.7788 0 0 1.00 frq= 3724.3701 xint= 0.6667
0.5 -0.0044 0.5 omi= 0.5 1 ---> 0.5 0.5 omf= 0.5 -1.0 0.0216 frq= 0.0260 1.3333 1.0034 0 0 1.00 frq= 3724.0460 xint= 1.3333
期望的输出示例:
3723.6699 0.5 -0.5 -1 1.5 0.5 -1.0 R_{-}^{+}(0.5)
3724.3701 1.5 0.5 -1 0.5 -0.5 -1.0 P_{+}^{-}(1.5)
3724.0460 0.5 0.5 1 0.5 0.5 -1.0 Q_{+}^{+}(0.5)
【问题讨论】:
标签: python arrays for-loop indexing