【问题标题】:Merge several lists with different length into a matrix将多个不同长度的列表合并成一个矩阵
【发布时间】:2012-12-08 08:10:56
【问题描述】:

输入:

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 中做到这一点?

【问题讨论】:

    标签: python arrays matrix numpy scipy


    【解决方案1】:
    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 () )
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:
        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)
        

        【讨论】:

          猜你喜欢
          • 2017-12-13
          • 2019-11-18
          • 2018-06-06
          • 2011-12-25
          • 1970-01-01
          • 2019-09-14
          • 1970-01-01
          • 2019-10-03
          • 1970-01-01
          相关资源
          最近更新 更多