在python中将数据转换为矩阵

时间:2018-02-01 07:13:30

标签: python numpy matrix

我有数据存储为data1.txt,看起来像这样

1,004,-59
1,004,-65
1,004,-69
1,005,-55
1,005,-57
1,006,-53
1,006,-59
1,007,-65
1,007,-69
1,007,-55
1,007,-57
1,008,-53
1,009,-59
1,009,-65
1,009,-69
1,009,-55
1,010,-57
1,010,-53
1,010,-59
1,010,-65
1,011,-69
1,011,-55
1,011,-57
1,011,-53

运行python代码将数据分隔成行,然后单独分开。

with open('data1.txt') as test:
    try:  
        test = open('data1.txt')
        line = test.readline()
        cnt = 1

        while line:
           print("Line {}: {}".format(cnt, line.strip()))
           data = line.split(',')
           line = test.readline()
           cnt += 1
           print(data[0],data[1],data[2])
    finally:  
        test.close()

但是每次第1列和第2列的值匹配时都希望将它展示为n * 3矩阵。我将如何使用numpy.matrix从这里开始?

期望的输出:

Line 1: 1,004,-59
Line 2: 1,004,-65
Line 3: 1,004,-69
[[1,004,-59],[1,004,-65],[1,004,-69]]
Line 4: 1,005,-55
Line 5: 1,005,-57
[[1,005,-55],[1,005,-57]]
.
.
.
.

等等。

1 个答案:

答案 0 :(得分:0)

我会寻求一个解决方案,我首先收集字典中的所有值。在下面的解决方案中,我假设值在文件中出现的顺序很重要。如果没有,您可以将OrderedDict替换为普通dict。另请注意,在Python 3.7版中,通常dict是有序的。

from collections import OrderedDict

input_dict = OrderedDict()

with open('data1.txt') as test:
    for line in test:
        data = list(map(int,line.split(',')))
        key = (data[0],data[1])
        input_dict.setdefault(key,[]).append(data)

for key,val in input_dict.items():
    print('{} : {}'.format(key, val))

给出输出

(1, 4) : [[1, 4, -59], [1, 4, -65], [1, 4, -69]]
(1, 5) : [[1, 5, -55], [1, 5, -57]]
(1, 6) : [[1, 6, -53], [1, 6, -59]]
(1, 7) : [[1, 7, -65], [1, 7, -69], [1, 7, -55], [1, 7, -57]]
(1, 8) : [[1, 8, -53]]
(1, 9) : [[1, 9, -59], [1, 9, -65], [1, 9, -69], [1, 9, -55]]
(1, 10) : [[1, 10, -57], [1, 10, -53], [1, 10, -59], [1, 10, -65]]
(1, 11) : [[1, 11, -69], [1, 11, -55], [1, 11, -57], [1, 11, -53]]

请注意,这些是列表(您显然想要的,从您的输出判断)。如果需要数组,请使用numpy并添加

for key, val in input_dict.items():
    input_dict[key] = np.array(val)
在打印出你的价值观之前

。希望这会有所帮助。