Python 将列放入数组,然后在循环中索引多个数组
我试图重新排列一些数据文件,将每一列放入自己的数组中,然后对其进行操作。稍后我将使用列数组的索引对行进行重新排序,但现在,我对循环以及如何使索引工作感到困惑 我目前的代码如下:Python 将列放入数组,然后在循环中索引多个数组,python,arrays,for-loop,indexing,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 = ope
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)
要对多个数组使用同一索引,请使用:
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语句和列数组的索引来更改行的顺序。祝你好运