Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x python:基于相似键的dict groupby列表,然后进行筛选_Python 3.x_List_Dictionary - Fatal编程技术网

Python 3.x python:基于相似键的dict groupby列表,然后进行筛选

Python 3.x python:基于相似键的dict groupby列表,然后进行筛选,python-3.x,list,dictionary,Python 3.x,List,Dictionary,我有一个目录,比如: data = [ {'11': {'x': 333, 'priority': 1, 'channels': 40}}, {'11': {'x': 444, 'priority': 2, 'channels': 30}}, {'22': {'x': 000, 'priority': 1, 'channels': 55}} ] 我想按具有类似键的dict元素分组,例如: [ [{'11': {'x': 333, 'priority': 1, 'chan

我有一个目录,比如:

data = [
  {'11': {'x': 333, 'priority': 1, 'channels': 40}}, 
  {'11': {'x': 444, 'priority': 2, 'channels': 30}}, 
  {'22': {'x': 000, 'priority': 1, 'channels': 55}}
]
我想按具有类似键的dict元素分组,例如:

 [
   [{'11': {'x': 333, 'priority': 1, 'channels': 40}}, 
    {'11': {'x': 444, 'priority': 2, 'channels': 30}}], 
   [{'22': {'x': 000, 'priority': 1, 'channels': 55}}]
 ]
然后,如果group by列表中有多个元素,则过滤名为
priority
的键上的dict元素

期望输出:

 [
   {'11': {'x': 333, 'priority': 1, 'channels': 40}}, 
   {'22': {'x': 000, 'priority': 1, 'channels': 55}}
 ]

可以使用循环执行此操作:

result = []
for group in data:
    result.append(min(group, key=lambda x: list(x.values())[0]['priority']))

可以使用循环执行此操作:

result = []
for group in data:
    result.append(min(group, key=lambda x: list(x.values())[0]['priority']))
印刷品:

[{'11': {'channels': 40, 'priority': 1, 'x': 333}},
 {'22': {'channels': 55, 'priority': 1, 'x': 0}}]
印刷品:

[{'11': {'channels': 40, 'priority': 1, 'x': 333}},
 {'22': {'channels': 55, 'priority': 1, 'x': 0}}]
你可以试试

from collections import defaultdict

res = defaultdict(list)
for i in data:
    res[list(i.keys())[0]].append(i)
print([min(i, key=lambda x: list(x.values())[0]['priority']) for i in res.values()])
输出

[{'11': {'x': 333, 'priority': 1, 'channels': 40}}, {'22': {'x': 0, 'priority': 1, 'channels': 55}}]
defaultdict
将使用相同的键聚合所有dict,然后使用
min
功能的列表比较将按min优先级返回dict。

您可以尝试

from collections import defaultdict

res = defaultdict(list)
for i in data:
    res[list(i.keys())[0]].append(i)
print([min(i, key=lambda x: list(x.values())[0]['priority']) for i in res.values()])
输出

[{'11': {'x': 333, 'priority': 1, 'channels': 40}}, {'22': {'x': 0, 'priority': 1, 'channels': 55}}]

defaultdict
将聚合具有相同键的所有dict,然后使用
min
函数的列表比较将按最小优先级返回dict。

它给出错误:
result.append(max(group,key=lambda x:list(x.values())[0]['priority'])AttributeError:'str'对象没有属性'values'
它给出错误:
result.append(max(group,key=lambda x:list(x.values())[0]['priority'])AttributeError:'str'对象没有属性'values'
是什么让您的示例中的元素“相似”?键
11
,或者它下面的数据中的某个东西?@9000键
11
使它相似。在您的示例中,什么使元素“相似”?键
11
,或者它下面的数据中的某个东西?@9000键
11
使它类似。太棒了,你能解释一下吗?@Jackma我创建了临时dict
tmp
,在这里我按键对列表
数据中的元素进行分组(我用
[*d][0]
提取键)。然后通过抓取每个组中的第一个子列表来生成输出列表。这个代码考虑了<代码>优先级<代码>值吗?当您更改“11”指令的顺序时,您将获得
优先级
2,而不是
优先级
1。太棒了,请您解释一下好吗?@Jackma我创建了临时指令
tmp
,其中我按键对列表
数据中的元素进行分组(我用
[*d][0]提取键)。然后通过抓取每个组中的第一个子列表来生成输出列表。这个代码考虑了<代码>优先级<代码>值吗?当您更改“11”指令的顺序时,您将获得
优先级
2而不是
优先级
1。此答案对您有帮助吗?此答案对您有帮助吗?