【问题标题】:Columns into array and then indexing multiple arrays in a loop列到数组中,然后在循环中索引多个数组
【发布时间】: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


    【解决方案1】:

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

    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 语句和列数组的索引来更改行的顺序。祝你好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-13
      • 2013-07-05
      • 2021-06-22
      • 1970-01-01
      • 2018-07-11
      • 2018-04-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多