Python 统计元组列表中的出现次数
我认为最好从输入和输出开始:Python 统计元组列表中的出现次数,python,stream,counter,Python,Stream,Counter,我认为最好从输入和输出开始: list_of_items = [ {"A": "abc", "B": "dre", "C": "ccp"}, {"A": "qwe", "B": "dre", "C": "ccp"}, {"A": "abc", "B": "dre", "C": "ccp"}, ] result = {'A-abc-->B': {'dre': 2}, 'A-abc-->C': {'ccp': 2}, 'A
list_of_items = [
{"A": "abc", "B": "dre", "C": "ccp"},
{"A": "qwe", "B": "dre", "C": "ccp"},
{"A": "abc", "B": "dre", "C": "ccp"},
]
result = {'A-abc-->B': {'dre': 2},
'A-abc-->C': {'ccp': 2},
'A-qwe-->B': {'dre': 1},
'A-qwe-->C': {'ccp': 1},
'B-dre-->A': {'abc': 2, 'qwe': 1},
'B-dre-->C': {'ccp': 3},
'C-ccp-->A': {'abc': 2, 'qwe': 1},
'C-ccp-->B': {'dre': 3}}
我的初始输入是以流形式出现的项目。这些条目基本上是带有键和值的字典。
我的目标是为每个特定的键获取最大值,并为其附带的所有其他键指定最大值
因此,如果在100个项目中,对于值为“1”的键“A”,我在键“B”中得到90个项目,值为“2”,在键“B”中得到10个项目,值为“1111”,我想看到一个列表,显示这些数字。B2=90,B1111=10
我的代码正在运行。
但是,我的现实生活场景包含大约20个键的100000多个不同值。
另外,我的最终目标是在Flink上运行此任务
因此,我正在寻找有关计数器/python流api的帮助
all_tuple_list_items = []
for dict_item in list_of_items:
list_of_tuples = [(k, v) for (k, v) in dict_item.items()]
all_tuple_list_items.append(list_of_tuples)
result_dict = {}
for list_of_tuples in all_tuple_list_items:
for id_tuple in list_of_tuples:
all_other_tuples = list_of_tuples.copy()
all_other_tuples.remove(id_tuple)
dict_of_specific_corresponding = {}
for corresponding_other_tu in all_other_tuples:
ids_connection_id = id_tuple[0] + "-" + str(id_tuple[1]) + "-->" + corresponding_other_tu[0]
corresponding_id = str(corresponding_other_tu[1])
if result_dict.get(ids_connection_id) is None:
result_dict[ids_connection_id] = {corresponding_id: 1}
else:
if result_dict[ids_connection_id].get(corresponding_id) is None:
result_dict[ids_connection_id][corresponding_id] = 1
else:
result_dict[ids_connection_id][corresponding_id] = result_dict[ids_connection_id][
corresponding_id] + 1
pprint(result_dict)
让它发挥作用。
但是,仍然希望得到一种更有效的方法。
使用计数器和流。
可能吗
代码
您可以使用函数
permutations()
生成dicts中项目的所有排列,并使用Counter
对其进行计数。最后,您可以使用defaultdict()
对计数器中的项目进行分组:
from collections import Counter, defaultdict
from itertools import permutations
from pprint import pprint
list_of_items = [
[{"A": "abc", "B": "dre", "C": "ccp"}],
[{"A": "qwe", "B": "dre", "C": "ccp"}],
[{"A": "abc", "B": "dre", "C": "ccp"}],
]
c = Counter(p for i in list_of_items
for p in permutations(i[0].items(), 2))
d = defaultdict(dict)
for ((i, j), (k, l)), num in c.items():
d[f'{i}-{j}-->{k}'][l] = num
pprint(d)
输出:
defaultdict(,
{'A-abc-->B':{'dre':2},
“A-abc-->C”:{'ccp':2},
'A-qwe-->B':{'dre':1},
'A-qwe-->C':{'ccp':1},
'B-dre-->A':{'abc':2,'qwe':1},
“B-dre-->C”:{'ccp':3},
'C-ccp-->A':{'abc':2,'qwe':1},
'C-ccp-->B':{'dre':3})
我知道我的标题与我描述的不完全一样。但是(这也是我的问题的一部分),我不知道如何描述我正在努力完成的任务。。(因此使用命令式而不是声明式编码。)您的数据结构非常奇怪:列表中只有一个dict,数字作为字符串。数字作为字符串只是键的一些值。这些可以是字母,也可以是其他的。。你对这一条的看法是对的。它会解决的。很难理解你需要什么。更好地解释您发布的示例。现在编辑,使其工作,但需要一个更优雅、更高效、更快的解决方案。1。该解决方案是否被认为是快速高效的(因此具有可扩展性)?因为你使用了现有的库?2.您能告诉我java中的等效库是什么吗?3.另外,这段代码对我的初始处理也很有用。但是现在我想每天用新数据更新我的计数器。使用计数器和默认dict是如何做到的?3的答案是使用c.update和来自新项目的新排列:)(阅读文档..)1。是的,我认为没有更好的办法了。Python库速度非常快。2.我不知道Java
。3.您可以使用新列表扩展list\u of_items.extend(new\u list)
,然后再次运行脚本。或者,如果您确实有大量数据,您可以使用
pickle`保存dict。然后您可以加载dict并将新列表放入计数器
。加载dict时,必须删除d=defaultdict(dict)
。否则它将覆盖您保存的dict。是的,您也可以这样做。但是我会更新您的最终dict(defaultdict
),而不是c
。您需要的是组合而不是排列。
from collections import Counter, defaultdict
from itertools import permutations
from pprint import pprint
list_of_items = [
[{"A": "abc", "B": "dre", "C": "ccp"}],
[{"A": "qwe", "B": "dre", "C": "ccp"}],
[{"A": "abc", "B": "dre", "C": "ccp"}],
]
c = Counter(p for i in list_of_items
for p in permutations(i[0].items(), 2))
d = defaultdict(dict)
for ((i, j), (k, l)), num in c.items():
d[f'{i}-{j}-->{k}'][l] = num
pprint(d)
defaultdict(<class 'dict'>,
{'A-abc-->B': {'dre': 2},
'A-abc-->C': {'ccp': 2},
'A-qwe-->B': {'dre': 1},
'A-qwe-->C': {'ccp': 1},
'B-dre-->A': {'abc': 2, 'qwe': 1},
'B-dre-->C': {'ccp': 3},
'C-ccp-->A': {'abc': 2, 'qwe': 1},
'C-ccp-->B': {'dre': 3}})