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])

毎回1行追加できる場合は、これが適しています。

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 に答える