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 如何使用groupby对列表中的元素进行分组和筛选?_Python_List_Group By - Fatal编程技术网

Python 如何使用groupby对列表中的元素进行分组和筛选?

Python 如何使用groupby对列表中的元素进行分组和筛选?,python,list,group-by,Python,List,Group By,我有以下清单: itemlist=[('ItemA','0','Type1'),('ItemA','0','Type2'),('ItemA','0','Type1'),('ItemB','0','Type2'),('ItemA','1','Type2'),('ItemB','1','Type3'),('ItemB','1','Type1'),('ItemC','1','Type4'),('ItemD','1','Type4')] 接下来,我按编号对项目进行分组,并计算编号: from ite

我有以下清单:


itemlist=[('ItemA','0','Type1'),('ItemA','0','Type2'),('ItemA','0','Type1'),('ItemB','0','Type2'),('ItemA','1','Type2'),('ItemB','1','Type3'),('ItemB','1','Type1'),('ItemC','1','Type4'),('ItemD','1','Type4')]

接下来,我按编号对项目进行分组,并计算编号:

from itertools import groupby

sortkeyfn_num = key = lambda s:s[0]
itemlist.sort(key=sortkeyfn_num)

result_name_dict = {}
for key,valuesiter in groupby(itemlist, key=sortkeyfn_num):
    result_name_dict[key] = tuple(v[1] for v in valuesiter)

res = {}
for k in result_name_dict.keys():
for i in result_name_dict.values()[result_name_dict.keys().index(k)]:
    res.setdefault(i, 0)
    res[i] += 1
print k,'=', res
res.clear()
结果:

ItemB = {'1': 2, '0': 1, '2': 1}
ItemC = {'1': 1}
ItemA = {'1': 1, '0': 3}
ItemD = {'1': 1}
但如何按数字和类型对项目进行分组,并计算结果中的类型? 结果必须是,例如:

ItemA 0: Type1 = 2
ItemA 0: Type2 = 1
ItemA 1: Type2 = 1
ItemB 0: Type2 = 1
ItemB 1: Type3 = 2
谢谢。

也许是这个

import collections
itemlist = [('ItemA', '0', 'Type1'), ('ItemA', '0', 'Type2'),('ItemA', '0', 'Type1'), ('ItemB', '0', 'Type2'), ('ItemA', '1', 'Type2'), ('ItemB', '2', 'Type1'), ('ItemB', '1', 'Type3'), ('ItemB', '1', 'Type1'), ('ItemC', '1', 'Type4'), ('ItemD', '1', 'Type4')]
data_dict = collections.defaultdict(int)
for attribute1, attribute2, attribute3 in itemlist:
    data_dict[(attribute1, attribute2, attribute3)] += 1
for key, value in sorted(data_dict.items()):
    attribute1, attribute2, attribute3 = key
    print("{attribute1} {attribute2}: {attribute3} = {value}".format(**locals()))

在这里使用会更有效:

输出:

ItemA 0: Type1 = 2
ItemA 0: Type2 = 1
ItemA 1: Type2 = 1
ItemB 0: Type2 = 1
ItemB 1: Type1 = 1
ItemB 1: Type3 = 1
ItemB 2: Type1 = 1
ItemC 1: Type4 = 1
ItemD 1: Type4 = 1

我们又来了。结束这个完全相同的问题的时间3:如果你想得到我们的帮助,那么你应该尊重这个网站的规则,并询问一个问题。然后,这里的编码员会非常乐意帮助你。你需要自己表现出一些努力来解决这个问题。所以这不是一个“请免费为我编码”的网站。我们帮助您进行编码。我们不为你编码。在我给出的链接中,请阅读“仍然离题问题”的示例3。另外,我想说的是,这里没有什么不愉快的感觉。一、 像其他投票结束你的问题的人一样,我只是想保持干净。对不起。这是我的密码。如果可以的话,请帮帮我。谢谢,我还有一个问题。但是“对不起,我们不再接受来自此帐户的问题”。我怎么能问?我必须做什么?它有效!非常感谢!)
ItemA 0: Type1 = 2
ItemA 0: Type2 = 1
ItemA 1: Type2 = 1
ItemB 0: Type2 = 1
ItemB 1: Type1 = 1
ItemB 1: Type3 = 1
ItemB 2: Type1 = 1
ItemC 1: Type4 = 1
ItemD 1: Type4 = 1