列进入数组,然后在循环中索引多个数组

时间:2014-09-15 23:32:27

标签: python arrays for-loop indexing

我正在尝试通过将每个列放入其自己的数组然后对其进行操作来重新排列某些数据文件。稍后我将使用列数组的索引来重新排序行,但是现在,我对循环以及如何使索引工作感到困惑。

我目前的代码如下:

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)

1 个答案:

答案 0 :(得分:0)

要对多个数组使用相同的索引,请使用:

for j in xrange(len(freq)):

然后,假设您的频率数组与所有其他数组具有相同的条目数,则应该遍历所有行。

您的代码也缺少" _ {+} ^ { - }"在每个分支中可能导致错误标记转换,因为if语句不会更正Tline条目并保留上一次运行的值以在新行中使用。

另外,对于if语句,最好不要使用==因为浮点数在存储十进制数时会出现舍入错误。

以下代码应该有助于解决问题:

import sys as sys
import struct
import decimal


#Small value for if statements
eps=1e-2

#Create empty sets to append with column values
freq = []
J_i = []
w_i = []
n_i = []
J_f = []
w_f = []
n_f = []
Tline = []

#Import data, split into columns you want, then assign column values to arrays
#3s = next three spaces are a string that I want to look at
#19x = skip the next 19 spaces in document
#Decimal used to keep last digit of frequency even if it is zero
#Used float to make numbers out of string. Can now do operations on numbers.
with open("path/to/file","r") as f:
    for line in f:
         (col0,col1,col2,col3,col4,col5,col6) = struct.unpack("3s19x4s2x2s8x3s9x4s5s65x10s14x",line.strip())
         freq.append(decimal.Decimal(col6))
         J_i.append(float(col0))
         w_i.append(float(col1))
         n_i.append(float(col2))
         J_f.append(float(col3))
         w_f.append(float(col4))
         n_f.append(float(col5))

#Define each line with the transition       
for j in xrange(len(freq)):
    if abs(J_f[j] - J_i[j]) < eps:
        if abs(w_i[j] - 0.5) < eps and abs(w_f[j] - 0.5) < eps:
            Tline.append("Q_{+}^{+}(")
        elif abs(w_i[j] + 0.5) < eps and abs(w_f[j] - 0.5) < eps:
            Tline.append("Q_{-}^{+}(")
        elif abs(w_i[j] - 0.5) < eps and abs(w_f[j] + 0.5) < eps:
            Tline.append("Q_{+}^{-}(")
        elif abs(w_i[j] + 0.5) < eps and abs(w_f[j] + 0.5) < eps:
            Tline.append("Q_{-}^{-}(")
    elif abs(J_f[j] - J_i[j] - 1) < eps:
        if abs(w_i[j] - 0.5) < eps and abs(w_f[j] - 0.5) < eps:
            Tline.append("R_{+}^{+}(")
        elif abs(w_i[j] + 0.5) < eps and abs(w_f[j] - 0.5) < eps:
            Tline.append("R_{-}^{+}(")
        elif abs(w_i[j] - 0.5) < eps and abs(w_f[j] + 0.5) < eps:
            Tline.append("R_{+}^{-}(")
        elif abs(w_i[j] + 0.5) < eps and abs(w_f[j] + 0.5) < eps:
            Tline.append("R_{-}^{-}(")
    elif abs(J_f[j] - J_i[j] + 1) < eps:
        if abs(w_i[j] - 0.5) < eps and abs(w_f[j] - 0.5) < eps:
            Tline.append("P_{+}^{+}(")
        elif abs(w_i[j] + 0.5) < eps and abs(w_f[j] - 0.5) < eps:
            Tline.append("P_{-}^{+}(")
        elif abs(w_i[j] - 0.5) < eps and abs(w_f[j] + 0.5) < eps:
            Tline.append("P_{+}^{-}(")
        elif abs(w_i[j] + 0.5) < eps and abs(w_f[j] + 0.5) < eps:
            Tline.append("P_{-}^{-}(")

#Rearrange the Columns, then
#Write out new arrangement with specified widths to align columns
sys.stdout = open('path/to/file', 'w')
for i in xrange(len(freq)):
    print '{:>9}  {:>3}  {:>4}  {:>4}  {:>3}  {:>4}  {:>4}  {:>10}{:>3}{:>1}'.format(freq[i], J_i[i], w_i[i], n_i[i], J_f[i], w_f[i], n_f[i], Tline[i], J_i[i], ")")
sys.stdout.close()

然后,您可以使用for语句和列数组的索引来更改行的顺序。祝你好运!