1

输入:

word_list = ["a", "b","c","d", "e"]
input1 = [("b",20),("a",10)}
input2 = [("c",9)]
input3 = [("d",70)]
result = merge_blabla(word_list, [input1, input2, input3])

如果可以每次添加一行,则此行会更好:

result = init_blabla(word_list)
result.append_blabla(input1)
result.append_blabla(input2)
result.append_blabla(input3)

输出是这样的:

result   
>> matrix(array(10,20,0,0,0), array(0,0,9,0,0), array(0,0,0,70,0))
result.colnames   
>> ["a", "b", "c", "d", "e"]

实际上word_list有 1M 个元素,结果是一个稀疏矩阵,所以效率可能很重要。

有没有人知道如何在 python 中做到这一点?

4

3 回答 3

0
class Matrix:
    def __init__ (self, columns):
        self.columns = columns
        self.rows = []

    def push (self, row):
        nRow = []
        row = dict (row)
        for key in self.columns:
            nRow.append (row [key] if key in row else 0)
        self.rows.append (nRow)

    def result (self): return self.rows

    def colNames (self): return self.columns

word_list = ["a", "b","c","d", "e"]
input1 = [("b",20),("a",10)]
input2 = [("c",9)]
input3 = [("d",70)]

m = Matrix (word_list)
m.push (input1)
m.push (input2)
m.push (input3)
print (m.result () )
print (m.colNames () )
于 2012-12-08T17:48:37.437 回答
0

使用DataFrame

>>> inputs
[('b', 20), ('a', 10), ('c', 9), ('d', 70)]
>>> data = {x[0]:[x[1]] for x in inputs}
>>> data
{'a': [10], 'c': [9], 'b': [20], 'd': [70]}
>>> results = pandas.DataFrame(data)
>>> results
    a   b  c   d
0  10  20  9  70
>>> results['e'] = [1]
>>> results
    a   b  c   d  e
0  10  20  9  70  1
>>> results.values
array([[10, 20,  9, 70,  1]], dtype=int64)
>>> results.columns
Index([a, b, c, d, e], dtype=object)
于 2012-12-08T21:05:24.247 回答
-1
class Matrix(object):
    def__init__(self, columns):
        self.columns = columns
        self.rows = []

    def insert_row(self, row):
        new_row = []
        for col in self.columns:
            for tup in row:
                if tup[0] == col:
                    new_row.append(tup[1])
                    break
            else:
                new_row.append(0)
        self.rows.append(new_row)
于 2012-12-08T09:53:30.003 回答