我在文件中有这个:
1 0 2 4 3
4 6 5 2 1
4 4 5 2 1
5 6 8 5 3
-
3 2 4 0
3 2 4 2
0 7 3 3
3 3 0 2
1 3 2 2
当行为'-'
时,我需要将每个分割成自己的矩阵。到目前为止我做了什么:
with open(fname) as f:
content = f.readlines()
subArray = []
firstMatrix= []
for line in content:
if line.strip() == '-':
break
for digit in line.split():
subArray.append(digit)
firstMatrix.append(subArray)
但是将'-'
之前的矩阵的所有数字放入数组数组中。我尝试在其中插入if line == '-' break
,但无济于事。最终输出需要是:
[[1, 0, 2, 4, 3],
[4, 6, 5, 2, 1],
[4, 4, 5, 2, 1],
[5, 6, 8, 5, 3]]
[[3, 2, 4, 0],
[3, 2, 4, 2],
[0, 7, 3, 3],
[3, 3, 0, 2],
[1, 3, 2, 2]]
每个都保存到各自的变量中:firstMatrix,secondMatrix。
答案 0 :(得分:3)
假设文件的全部内容都是可变内容
url(ur'^reply/(.*)/?$', views.visit),
答案 1 :(得分:3)
您可以将其实现为生成器。
def get_matrix(filename):
with open(filename, 'r') as fin:
matrix = []
for line in fin:
line = line.strip()
if line == '-':
yield matrix
matrix = []
else:
matrix.append([int(i) for i in line.split()])
else:
yield matrix # yield last matrix in case file doesn't end with '-'
>>> for m in get_matrix('matrix_file.txt'):
print(m)
[[1, 0, 2, 4, 3], [4, 6, 5, 2, 1], [4, 4, 5, 2, 1], [5, 6, 8, 5, 3]]
[[3, 2, 4, 0], [3, 2, 4, 2], [0, 7, 3, 3], [3, 3, 0, 2], [1, 3, 2, 2]]
答案 2 :(得分:1)
current = 0
curr_mat = []
all_matrix = []
with open(fname) as f:
line = f.readlines()
line = line.split()
if len(line) == 1 and line[0] == '-':
count += 1
all_matrix.append(curr_mat)
curr_mat = []
else:
curr_mat.append(line)
all_matrix将处理后的所有矩阵,索引0
将具有matrix0,1
索引将具有matrix1,依此类推。
答案 3 :(得分:1)
你不能在列表理解中使用break
您应首先处理数据
B2
matrix [0]包含第一个矩阵.. etc
答案 4 :(得分:1)
这很有效。
将所有矩阵保存到数组而不是变量名。
f = open('file1.txt')
matrices = []
matrix = []
for line in f.readlines():
str = line.translate(None, '\n\r ')
if(str[0] != '-'):
matrix.append(list(str))
else:
matrices.append(matrix)
matrix = []
#depends if code ends in '-' char
matrices.append(matrix)
print(matrices)
答案 5 :(得分:1)
您永远不会在代码中重置subArray
:
for digit in line.split():
subArray.append(digit)
firstMatrix.append(subArray)
因此subArray
将包含矩阵中的所有数字。
试试这个:
matrices = [[] for _ in range(2)] # since you only have 2 matrices in text file
no_matrix = 0 # matrices[no_matrix], you get the idea
with open(fname) as f:
content = f.readlines()
for line in content:
subArray = []
if line.strip() == '-':
no_matrix += 1
else:
for digit in line.split():
subArray.append(digit)
matrices[no_matrix].append(subArray)
for matrix in matrices:
print(matrix)
干杯!
答案 6 :(得分:1)
这对我有用:
txt = open('stackFile.txt')
file = txt.read()
splitedFile = file.split(' ')
# print m
allmatrix = []
data = []
c = list()
for n in splitedFile:
# print "n",n
if '\n' in n:
if '-' in n:
v = n.split('\n-\n')
c.append(v[0])
data.append(c)
allmatrix.append(data)
c = list()
data = list()
c.append(v[1])
else:
v = n.split('\n')
c.append(v[0])
data.append(c)
c = list()
c.append(v[1])
else:
c.append(n)
updateFinalMatrix = allmatrix.append(data)
for matrix in allmatrix:
print matrix
答案 7 :(得分:1)
这只是对@Arjun的帖子的澄清(即在短划线末尾注明没有“\ n”):
import re
content="\
1 0 2 4 3\n\
4 6 5 2 1\n\
4 4 5 2 1\n\
5 6 8 5 3\n\
-\
3 2 4 0\n\
3 2 4 2\n\
0 7 3 3\n\
3 3 0 2\n\
1 3 2 2\n"
matrices = re.split('-', content)
firstMatrix, secondMatrix = [[mat.split() for mat in matrix.splitlines()] for matrix in matrices]
print(firstMatrix,"\n",secondMatrix)
这为您提供了所需的答案:
[['1', '0', '2', '4', '3'], ['4', '6', '5', '2', '1'], ['4', '4', '5', '2', '1'], ['5', '6', '8', '5', '3']]
[['3', '2', '4', '0'], ['3', '2', '4', '2'], ['0', '7', '3', '3'], ['3', '3', '0', '2'], ['1', '3', '2', '2']]