【问题标题】:Grouping by key value property in List of dict python按字典python列表中的键值属性分组
【发布时间】:2019-06-29 04:03:09
【问题描述】:

我有 orig 包含 dicttext 值的元组列表。

orig = [({'x': 28.346, 'y': 19},'Text0'),
 ({'x': 109.726, 'y': 19},'Text1'),
 ({'x': 147.776, 'y': 19},'Text2'),
 ({'x': 153.606, 'y': 24}, 'Text3'),
 ({'x': 452.788, 'y': 24}, 'Text4'),
 ({'x': 504.168, 'y': 34}, 'Text5'),
 ({'x': 527.768, 'y': 34}, 'Text6'),
 ({'x': 533.598, 'y': 45},'Text7'),
 ({'x': 64.291, 'y': 55},'Text8'),
 ({'x': 98.623, 'y': 55},'Text9')]

我想从key='y' 中过滤组,这将使我根据y 中的唯一值列出它。类似于以下内容:

res = [
      [({'x': 28.346, 'y': 19},'Text0'),
         ({'x': 109.726, 'y': 19},'Text1'),
         ({'x': 147.776, 'y': 19},'Text2')],
     [({'x': 153.606, 'y': 24}, 'Text3'),
         ({'x': 452.788, 'y': 24}, 'Text4')],
     [({'x': 504.168, 'y': 34}, 'Text5'),
         ({'x': 527.768, 'y': 34}, 'Text6')],
     [({'x': 533.598, 'y': 45},'Text7')],
     [({'x': 64.291, 'y': 55},'Text8'),
         ({'x': 98.623, 'y': 55},'Text9')]]

【问题讨论】:

  • 为什么 y 的值在列表的第一项中不是唯一的。 'y' 的值是 19、24、34、45,而在第二项中,'y' 是唯一的只有 55
  • 更新了问题。
  • 'res'主列表中有两项,主列表[({'x': 28.346, 'y': 19},'Text0'), ({'x': 109.726, 'y': 19},'Text1'), ({'x': 147.776, 'y': 19},'Text2'), ({'x': 153.606, 'y': 24}, 'Text3'), ({'x': 452.788, 'y': 24}, 'Text4'), ({'x': 504.168, 'y': 34}, 'Text5'), ({'x': 527.768, 'y': 34}, 'Text6'), ({'x': 533.598, 'y': 45},'Text7')]的第一项中y的值不唯一。希望你能理解

标签: python json list dictionary filter


【解决方案1】:

如果你使用numpy 会更容易一些。

import numpy as np
orig = [({'x': 28.346, 'y': 19}, 'Text0'),
        ({'x': 109.726, 'y': 19}, 'Text1'),
        ({'x': 147.776, 'y': 19}, 'Text2'),
        ({'x': 153.606, 'y': 24}, 'Text3'),
        ({'x': 452.788, 'y': 24}, 'Text4'),
        ({'x': 504.168, 'y': 34}, 'Text5'),
        ({'x': 527.768, 'y': 34}, 'Text6'),
        ({'x': 533.598, 'y': 45}, 'Text7'),
        ({'x': 64.291, 'y': 55}, 'Text8'),
        ({'x': 98.623, 'y': 55}, 'Text9')]


input_array = np.array([val[0]['y'] for val in orig])
out_array = [np.where(input_array == element)[0].tolist() for element in np.unique(input_array)]
res = [[orig[i] for i in ind_arr] for ind_arr in out_array]
print(res)

输出:

[[({'x': 28.346, 'y': 19}, 'Text0'),
  ({'x': 109.726, 'y': 19}, 'Text1'),
  ({'x': 147.776, 'y': 19}, 'Text2')],
 [({'x': 153.606, 'y': 24}, 'Text3'),
    ({'x': 452.788, 'y': 24}, 'Text4')],
 [({'x': 504.168, 'y': 34}, 'Text5'),
    ({'x': 527.768, 'y': 34}, 'Text6')],
 [({'x': 533.598, 'y': 45}, 'Text7')],
 [({'x': 64.291, 'y': 55}, 'Text8'),
    ({'x': 98.623, 'y': 55}, 'Text9')]]

【讨论】:

  • 没有。我想要列表中具有唯一值 y 的列表。
  • 你能分享你想要的输出吗,我猜这个问题看起来很混乱
  • 我已经更新了这个问题。 origlist 类型,其中 reslistlist。基于y 的唯一值。
  • 为什么 y 的值在列表的第一项中不是唯一的。 'y' 的值是 19、24、34、45,而在第二项中,'y' 是唯一的只有 55
  • 哇。这是使用numpy 的好答案。
【解决方案2】:

使用itertools.groupbylist comprehension 的两行解决方案:

from itertools import groupby
# group by the input orig with a key of dict "y" and then take it in a list of list comprehension
print ([[x for x in v] for k, v in groupby(orig, key= lambda x: x[0]["y"])])

结果:

[[({'x': 28.346, 'y': 19}, 'Text0'), ({'x': 109.726, 'y': 19}, 'Text1'), ({'x': 147.776, 'y': 19}, 'Text2')], [({'x': 153.606, 'y': 24}, 'Text3'), ({'x': 452.788, 'y': 24}, 'Text4')], [({'x': 504.168, 'y': 34}, 'Text5'), ({'x': 527.768, 'y': 34}, 'Text6')], [({'x': 533.598, 'y': 45}, 'Text7')], [({'x': 64.291, 'y': 55}, 'Text8'), ({'x': 98.623, 'y': 55}, 'Text9')]]

我希望这很重要:)

【讨论】:

  • 这太野蛮了。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多