[英]Counting “unique pairs” of numbers into a python dictionary?
EDIT: Edited typos; 编辑:编辑错别字; the key values of the dictionary should be dictionaries, not sets. 字典的键值应该是字典,而不是集合。
I will keep the typos here though, as the questions below address this question. 我将在这里保留输入错误,因为以下问题解决了这个问题。 My apologies for the confusion. 对于造成混乱,我深表歉意。
Here's the problem: 这是问题所在:
Let's say I have a list of integers whereby are never repeats: 假设我有一个整数列表,其中永不重复:
list1 = [2, 3]
In this case, there is a unique pair 2-3 and 3-2, so the dictionary should be: 在这种情况下,存在唯一的一对2-3和3-2,因此字典应为:
{2:{3: 1}, 3:{2: 1}}
That is, there is 1 pair of 2-3 and 1 pair of 3-2. 即,存在一对2-3和1对3-2。
For larger lists, the pairing is the same, eg 对于较大的列表,配对是相同的,例如
list2 = [2, 3, 4]
has the dicitonary 具有二分法
{2:{3: 1}, 3:{2: 1}, 3:{4: 1}, 4:{3: 1}, 2:{4: 1}, 4:{2: 1}}
(1) Once the size of the lists become far larger, how would one algorithmically find the "unique pairs" in this format using python data structures? (1)一旦列表的大小变得更大,一个人如何使用python数据结构在算法上找到这种格式的“唯一对”?
(2) I mentioned that the lists cannot have repeat integers, eg (2)我提到列表不能有重复整数,例如
[2, 2, 3]
is impossible, as there are two 2s. 这是不可能的,因为有两个2。
However, one may have a list of lists: 但是,可能会有一个列表列表:
list3 = [[2, 3], [2, 3, 4]]
whereby the dictionary must be 字典必须是
{2:{3: 2}, 3:{2: 2}, 3:{4: 1}, 4:{3: 1}, 2:{4: 1}, 4:{2: 1}}
as there are two pairs of 2-3 and 3-2. 因为有两对2-3和3-2。 How would one "update" the dictionary given multiple lists within a list? 给定一个列表中的多个列表,如何“更新”字典?
This is an algorithmic problem, and I don't know of the most efficient solution. 这是一个算法问题,我不知道最有效的解决方案。 My idea would be to somehow cache values in a list and enumerate pairs...but that would be so slow. 我的想法是以某种方式将值缓存在列表中并枚举对……但这太慢了。 I'm guessing there's something useful from itertools
. 我猜itertools
有一些有用的东西。
What you want is to count pairs that arise from combinations in your lists. 您想要的是对列表中组合产生的对进行计数。 You can find those with a Counter
and combinations
. 您可以找到带有Counter
和combinations
。
from itertools import combinations
from collections import Counter
list2 = [2, 3, 4]
count = Counter(combinations(list2, 2))
print(count)
Counter({(2, 3): 1, (2, 4): 1, (3, 4): 1})
As for your list of list, we update the Counter
with the result from each sublist. 至于您的清单清单,我们会使用每个子清单的结果来更新Counter
。
from itertools import combinations
from collections import Counter
list3 = [[2, 3], [2, 3, 4]]
count = Counter()
for sublist in list3:
count.update(Counter(combinations(sublist, 2)))
print(count)
Counter({(2, 3): 2, (2, 4): 1, (3, 4): 1})
My approach iterates over the input dict
(linear complexity) and pairs each key with its first available integer (this complexity depends on the exact specs of your question - eg, can each list contain unlimited sub-lists?), inserting these into an output dict (constant complexity). 我的方法遍历输入dict
(线性复杂度),并将每个键与它的第一个可用整数配对(此复杂度取决于您问题的确切规格-例如,每个列表是否可以包含无限的子列表?),然后将它们插入到输出中dict(恒定复杂度)。
import os
import sys
def update_results(result_map, tup):
# Update dict inplace
# Don't need to keep count here
try:
result_map[tup] += 1
except KeyError:
result_map[tup] = 1
return
def algo(input):
# Use dict to keep count of unique pairs while iterating
# over each (key, v[i]) pair where v[i] is an integer in
# list input[key]
result_map = dict()
for key, val in input.items():
key_pairs = list()
if isinstance(val, list):
for x in val:
if isinstance(x, list):
for y in x:
update_results(result_map, (key, y))
else:
update_results(result_map, (key, x))
else:
update_results(result_map, (key, val))
return len(result_map.keys())
>>> input = { 1: [1, 2], 2: [1, 2, [2, 3]] }
>>> algo(input)
>>> 5
I'm pretty sure there's a more refined way to do this (again, would depend on the exact specs of your question), but this could get your started (no imports) 我敢肯定,还有一种更精致的方法可以做到这一点(再次,这取决于您所问问题的具体规格),但这可以使您入门(无需导入)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.