将多个不同长度的列表合并成一个矩阵

2024-09-28 21:17:40 发布

您现在位置:Python中文网/ 问答频道 /正文

输入:

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

如果每次都可以添加一行,则这一个更好:

^{pr2}$

输出如下:

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中实现这一点吗?在


Tags: 元素矩阵resultmergearraymatrixlistword
3条回答
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 () )
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)

使用^{}

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

相关问题 更多 >