【问题标题】:How to iterate through matrix and change floats through calculation?如何遍历矩阵并通过计算改变浮点数?
【发布时间】:2020-01-31 20:01:10
【问题描述】:

我有以下清单:

N_division_n = [0.0, 1.0, 0.4150374992788437]

我有以下矩阵:

sample_collection =
[['', 'test1.txt', 'test2.txt', 'test3.txt', 'test4.txt'], 
['apple', 1.0, 1.0, 1.0, 1.0], 
['banana', 1;0, 1.0, 0.0, 0.0], 
['lemon', 1.0, 0.0, 2.0, 1.0]]

我想将矩阵行中的所有值与前面列表中的相应值相乘。像这样:apple values * 0.0, banana values * 1.0, lemon values * 0.4150374992788437

我尝试构建以下内容,但它不想遍历浮点数,我不知道如何修复它。

def tf_df_calculation(sample_collection):
    for a in range(1,len(sample_collection)):
        tf_df_list = [item[a] for item in N_division_n]
        value_a = 0
        for i in tf_df_list:
            if type(i) is float:
                value_a = i * N_division_n
                sample_collection.append(value_a)

tf_df_calculation(sample_collection)

预期输出:

[['', 'test1.txt', 'test2.txt', 'test3.txt', 'test4.txt'], 
['apple',  0.0, 0.0, 0.0, 0.0], 
['banana',  1.0, 1.0, 0.0, 0.0], 
['lemon',  0.41503749927884376, 0.0, 0.8300749985576875, 0.41503749927884376]]

【问题讨论】:

    标签: python python-3.x list matrix


    【解决方案1】:

    这个简单的函数可以工作,但它使用了两个 for 循环。无论如何,如果您的矩阵很小,这不是问题。

    N_division_n = [0.0, 1.0, 0.4150374992788437]
    
    sample_collection = [['', 'test1.txt', 'test2.txt', 'test3.txt', 'test4.txt'], 
    ['apple', 1.0, 1.0, 1.0, 1.0], 
    ['banana', 1.0, 1.0, 0.0, 0.0], 
    ['lemon', 1.0, 0.0, 2.0, 1.0]]
    
    def tf_df_calculation(sample_collection):
        for a in range(1,len(sample_collection)):
            for b in range(1,len(sample_collection[a])):
                sample_collection[a][b] *= N_division_n[a-1]
    
    tf_df_calculation(sample_collection)
    
    print(sample_collection)
    
    

    输出

    [['', 'test1.txt', 'test2.txt', 'test3.txt', 'test4.txt'], ['apple', 0.0, 0.0, 0.0, 0.0], ['banana', 1.0, 1.0, 0.0, 0.0], ['lemon', 0.4150374992788437, 0.0, 0.8300749985576874, 0.4150374992788437]]
    

    【讨论】:

      【解决方案2】:

      给你:

      N_division_n = [0.0, 1.0, 0.4150374992788437]
      
      sample_collection =[['', 'test1.txt', 'test2.txt', 'test3.txt', 'test4.txt'], ['apple', 1.0, 1.0, 1.0, 1.0], ['banana', 1.0, 1.0, 0.0, 0.0], ['lemon', 1.0, 0.0, 2.0, 1.0]]
      
      results = [sample_collection[0]]
      count = 0
      for sample in sample_collection[1::]:
          calculated_line = [sample[0]] + [x*N_division_n[count] for x in sample[1::]]
          results.append(calculated_line)
          count += 1
      

      输出:

      [['', 'test1.txt', 'test2.txt', 'test3.txt', 'test4.txt'], ['apple', 0.0, 0.0, 0.0, 0.0], ['banana', 1.0, 1.0, 0.0, 0.0], ['lemon', 0.4150374992788437, 0.0, 0.8300749985576874, 0.4150374992788437]]
      

      【讨论】:

      • 您可以在循环中使用enumerate 来避免使用计数器。例如:for i, sample in enumerate(sample_collection[1::])。然后用i代替counter,会自动递增。
      猜你喜欢
      • 2022-01-12
      • 2013-01-17
      • 2021-05-22
      • 2018-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-14
      • 2021-02-14
      相关资源
      最近更新 更多