如何从给定文件中读取矩阵?

发布于 2024-12-07 06:30:29 字数 875 浏览 0 评论 0原文

我有一个文本文件,其中包含 N * M 维度的矩阵。

例如,input.txt 文件包含以下内容:

0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0
0,0,2,1,0,2,0,0,0,0
0,0,2,1,1,2,2,0,0,1
0,0,1,2,2,1,1,0,0,2
1,0,1,1,1,2,1,0,2,1

我需要编写 python 脚本,在其中可以导入矩阵。

我当前的 python 脚本是:

f = open ( 'input.txt' , 'r')
l = []
l = [ line.split() for line in f]
print l

输出列表是这样的

[['0,0,0,0,0,0,0,0,0,0'], ['0,0,0,0,0,0,0,0,0,0'], ['0,0,0,0,0,0,0,0,0,0'],
 ['0,0,0,0,0,0,0,0,0,0'], ['0,0,0,0,0,0,0,0,0,0'], ['0,0,0,0,0,0,0,0,0,0'],
 ['0,0,2,1,0,2,0,0,0,0'], ['0,0,2,1,1,2,2,0,0,1'], ['0,0,1,2,2,1,1,0,0,2'],
 ['1,0,1,1,1,2,1,0,2,1']]

我需要获取 int 形式的值。如果我尝试输入强制类型转换,它会抛出错误。

I have a text file which contains matrix of N * M dimensions.

For example the input.txt file contains the following:

0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0
0,0,2,1,0,2,0,0,0,0
0,0,2,1,1,2,2,0,0,1
0,0,1,2,2,1,1,0,0,2
1,0,1,1,1,2,1,0,2,1

I need to write python script where in I can import the matrix.

My current python script is:

f = open ( 'input.txt' , 'r')
l = []
l = [ line.split() for line in f]
print l

the output list comes like this

[['0,0,0,0,0,0,0,0,0,0'], ['0,0,0,0,0,0,0,0,0,0'], ['0,0,0,0,0,0,0,0,0,0'],
 ['0,0,0,0,0,0,0,0,0,0'], ['0,0,0,0,0,0,0,0,0,0'], ['0,0,0,0,0,0,0,0,0,0'],
 ['0,0,2,1,0,2,0,0,0,0'], ['0,0,2,1,1,2,2,0,0,1'], ['0,0,1,2,2,1,1,0,0,2'],
 ['1,0,1,1,1,2,1,0,2,1']]

I need to fetch the values in int form . If I try to type cast, it throws errors.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

春庭雪 2024-12-14 06:30:29

考虑

with open('input.txt', 'r') as f:
    l = [[int(num) for num in line.split(',')] for line in f]
print(l)

产生

[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 2, 1, 0, 2, 0, 0, 0, 0], [0, 0, 2, 1, 1, 2, 2, 0, 0, 1], [0, 0, 1, 2, 2, 1, 1, 0, 0, 2], [1, 0, 1, 1, 1, 2, 1, 0, 2, 1]]

注意,你必须用逗号分隔。


如果确实有空行,则更

l = [[int(num) for num in line.split(',')] for line in f ]

改为

l = [[int(num) for num in line.split(',')] for line in f if line.strip() != "" ]

Consider

with open('input.txt', 'r') as f:
    l = [[int(num) for num in line.split(',')] for line in f]
print(l)

produces

[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 2, 1, 0, 2, 0, 0, 0, 0], [0, 0, 2, 1, 1, 2, 2, 0, 0, 1], [0, 0, 1, 2, 2, 1, 1, 0, 0, 2], [1, 0, 1, 1, 1, 2, 1, 0, 2, 1]]

Note that you have to split on commas.


If you do have blank lines then change

l = [[int(num) for num in line.split(',')] for line in f ]

to

l = [[int(num) for num in line.split(',')] for line in f if line.strip() != "" ]
夜灵血窟げ 2024-12-14 06:30:29

您可以简单地使用numpy.loadtxt
易于使用,您还可以指定分隔符、数据类型等。

具体来说,您需要做的就是:

import numpy as np
input = np.loadtxt("input.txt", dtype='i', delimiter=',')
print(input)

输出将是:

[[0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 2 1 0 2 0 0 0 0]
 [0 0 2 1 1 2 2 0 0 1]
 [0 0 1 2 2 1 1 0 0 2]
 [1 0 1 1 1 2 1 0 2 1]]

You can simply use numpy.loadtxt.
Easy to use, and you can also specify your delimiter, datatypes etc.

specifically, all you need to do is this:

import numpy as np
input = np.loadtxt("input.txt", dtype='i', delimiter=',')
print(input)

And the output would be:

[[0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 2 1 0 2 0 0 0 0]
 [0 0 2 1 1 2 2 0 0 1]
 [0 0 1 2 2 1 1 0 0 2]
 [1 0 1 1 1 2 1 0 2 1]]
隔岸观火 2024-12-14 06:30:29

你可以这样做:

fin = open('input.txt','r')
a=[]
for line in fin.readlines():
    a.append( [ int (x) for x in line.split(',') ] )

You can do this:

fin = open('input.txt','r')
a=[]
for line in fin.readlines():
    a.append( [ int (x) for x in line.split(',') ] )
微凉 2024-12-14 06:30:29

以下内容可以满足您的要求:

l = []
with open('input.txt', 'r') as f:
  for line in f:
    line = line.strip()
    if len(line) > 0:
      l.append(map(int, line.split(',')))
print l

The following does what you want:

l = []
with open('input.txt', 'r') as f:
  for line in f:
    line = line.strip()
    if len(line) > 0:
      l.append(map(int, line.split(',')))
print l
薄暮涼年 2024-12-14 06:30:29

您不应该编写 csv 解析器,在读取此类文件时考虑 csv 模块,并在读取后使用 with 语句关闭:

import csv

with open('input.txt') ad f:
    data = [map(int, row) for row in csv.reader(f)]

You should not write your csv parser, consider the csv module when reading such files and use the with statement to close after reading:

import csv

with open('input.txt') ad f:
    data = [map(int, row) for row in csv.reader(f)]
爱殇璃 2024-12-14 06:30:29

查看这个用于读取矩阵的一行小代码,

matrix = [[input() for x in range(3)] for y in range(3)]

该代码将读取 3*3 阶的矩阵。

Check out this small one line code for reading matrix,

matrix = [[input() for x in range(3)] for y in range(3)]

this code will read matrix of order 3*3.

桃扇骨 2024-12-14 06:30:29
import numpy as np
f = open ( 'input.txt' , 'r')
l = []
l = np.array([ line.split() for line in f])
print (l)
type(l)

输出:

<预><代码>[['0'] ['0'] ['0'] ['0,0,0,0,0,0,0,0,0,0,0']
['0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0']
['0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0']]

numpy.ndarray

import numpy as np
f = open ( 'input.txt' , 'r')
l = []
l = np.array([ line.split() for line in f])
print (l)
type(l)

output:

[['0']  ['0']  ['0']  ['0,0,0,0,0,0,0,0,0,0,0'] 
    ['0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0'] 
['0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0']] 

numpy.ndarray

我三岁 2024-12-14 06:30:29

以下代码将上述输入转换为矩阵形式:

f = open ('input.txt' , 'r')
l = []
l = [line.split() for line in f]
l=np.array(l)
l=l.astype(np.int)

The following code converts the above input to matrix form:

f = open ('input.txt' , 'r')
l = []
l = [line.split() for line in f]
l=np.array(l)
l=l.astype(np.int)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文