输入:
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中执行此操作的想法?
答案 0 :(得分: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 () )
答案 1 :(得分: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)
答案 2 :(得分:-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)