【问题标题】:Using Python to find matching arrays and combining into one array使用 Python 查找匹配的数组并组合成一个数组
【发布时间】:2020-03-15 19:30:41
【问题描述】:

我想使用 Python 来查找匹配的数组,例如 [4012630, 0.07575758][4012630, 0.5671642]。然后我想将它们组合成1个数组并添加小数。所以它会变成[4012630, 0.64292178]

目标是转换这个数组:

[[4012630, 0.07575758], 
[4012618, 0.014925373], 
[4012630, 0.5671642], 
[4012624, 0.029850746], 
[4012628, 0.41791046], 
[4012624, 0.07462686], 
[4012628, 0.04477612], 
[4012636, 0.2820513]]

进入这个数组:

[[4012630, 0.64292178],
[4012618, 0.014925373],
[4012624, 0.104477606],
[4012628, 0.46268658,
[4012636, 0.2820513]]

【问题讨论】:

标签: python arrays python-3.x


【解决方案1】:

有很多方法可以解决这个问题,这里是我的 O(n) 时间复杂度解决方案:

def unique_sum(input_list):
  index_mapping = {}
  final_list = []
  for i in range(0, len(input_list)):
    if input_list[i][0] in index_mapping: 
      index = index_mapping[input_list[i][0]]
      final_list[index][1] += input_list[i][1]
    else:
      index_mapping[input_list[i][0]] = len(final_list)
      final_list.append([input_list[i][0], input_list[i][1]])
  return final_list

然后你可以像这样调用这个函数:

data = [[4012630, 0.07575758], 
[4012618, 0.014925373], 
[4012630, 0.5671642], 
[4012624, 0.029850746], 
[4012628, 0.41791046], 
[4012624, 0.07462686], 
[4012628, 0.04477612], 
[4012636, 0.2820513]]


print(unique_sum(data))

【讨论】:

    【解决方案2】:

    我提出以下解决方案。首先,找出独特的标签。然后创建结果列表并将总和初始化为0。然后,迭代输入列表中的每个元素,并将值添加到相应的bin中。

    lst = [[4012630, 0.07575758],
    [4012618, 0.014925373],
    [4012630, 0.5671642],
    [4012624, 0.029850746],
    [4012628, 0.41791046],
    [4012624, 0.07462686],
    [4012628, 0.04477612],
    [4012636, 0.2820513]]
    
    def sumlist(lst):
        unique = list(set([x[0] for x in lst]))
        result = [[x,0] for x in unique]
        for i, value in lst:
            ind = unique.index(i)
            result[ind][1] += value
        return result
    

    【讨论】:

    • 谢谢!非常简单的解决方案,我能够完全理解它!
    【解决方案3】:

    您可以将字典数据结构用于以下任务。

    这是我的使用方法-

    data = [[4012630, 0.07575758],
    [4012618, 0.014925373],
    [4012630, 0.5671642],
    [4012624, 0.029850746],
    [4012628, 0.41791046],
    [4012624, 0.07462686],
    [4012628, 0.04477612],
    [4012636, 0.2820513]]
    
    dict={}    #Declaring an empty dictionary.
    for nat,dec in data:
        if nat not in dict:
            dic[nat]=dec
        else:
            dic[nat]+=dec
    print(dict)
    

    代码的输出是-

    {4012630: 0.64292178, 
    4012618: 0.014925373, 
    4012624: 0.104477606, 
    4012628: 0.46268657999999996, 
    4012636: 0.2820513}
    

    这里唯一的问题是输出将是字典而不是数组/列表。但如果需要,您可以轻松地将字典转换为数组。

    【讨论】:

      【解决方案4】:
      from collections import defaultdict
      def sum_func(l:list):
          temp1 = defaultdict(list)
          result = []
          for el in l:
              temp1[el[0]].append(el[1])
          for k, v in temp1.items():
              result.append([k, sum(v)])
          return result
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-14
        • 1970-01-01
        • 1970-01-01
        • 2021-07-18
        • 2020-06-24
        相关资源
        最近更新 更多