gpt4 book ai didi

python - 在python中查找二维数组中一对数字的频率

转载 作者:太空狗 更新时间:2023-10-30 02:43:45 25 4
gpt4 key购买 nike

我想在二维数组中找到对的频率。输入示例如下:

list_of_items = [[12,14,18],[12,19,54,89,105],[ 14, 19],[54, 88 ,105,178]]

预期输出如下:

(12,14):1
(12,18):1
(12,19):1
(12,54):1
(12,88):0
.
.
.
(54,105):2
.
.

我试过以下代码,但我认为这不是最佳解决方案:

number_set = [ 12, 14, 18,19,54,88,89 , 105, 178]

def get_frequency_of_pairs(list_of_items, number_set):
x=1
combination_list = []
result = {}
for i in number_set:
for j in range(x,len(number_set)):
combination_list = combination_list +[(i,number_set[j])]
x = x+1
for t in combination_list:
result[t]=0
for t in combination_list:
for items in list_of_items:
if( set(t).issubset(items) ):
result[t]=result[t]+1
return result

最佳答案

您可以使用 itertools 中的组合并使用集合中的计数器,如下所示:

counts = collections.Counter()
list_of_items = [[12,14,18], [12,19,54,89,105], [14,19], [54,88,105,178]]
for sublist in list_of_items:
counts.update(itertools.combinations(sublist, 2))

print counts
Counter({(54, 105): 2, (88, 105): 1, (54, 89): 1, (19, 105): 1, (12, 14): 1, (14, 19): 1, (14, 18): 1, (12, 89): 1, (12, 19): 1, (89, 105): 1, (12, 18): 1, (19, 89): 1, (19, 54): 1, (105, 178): 1, (88, 178): 1, (54, 178): 1, (12, 105): 1, (12, 54): 1, (54, 88): 1})

每一对都必须被枚举才能被计数,这种方法使您能够每对仅枚举一次。应该是最好的时间复杂度。

关于python - 在python中查找二维数组中一对数字的频率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32203260/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com