根据相互数量对网络中用户之间的连接进行分组

Grouping connections between users in a network based on number of mutuals

我有一个元组列表。每个元组代表社交网络中的一个人。第一项是他们的 ID 或“名称”。第二个是字典;每个键都是网络中与他们有相互联系的另一个人,其值是他们有多少共同点。

network = [
    (6, {3: 3, 4: 3, 7: 2, 1: 3, 11: 2}),
    (1, {7: 3, 11: 4, 6: 3, 4: 3}),
    (4, {3: 2, 6: 3, 1: 3, 11: 2, 12: 3}),
    (2, {9: 4, 8: 2, 10: 2, 5: 2}),
    (12, {3: 2, 4: 3}),
    (3, {5: 2, 8: 2, 12: 2, 4: 2, 7: 2, 6: 3}),
    (10, {2: 2, 9: 3, 8: 3, 5: 2}),
    (5, {3: 2, 8: 3, 9: 4, 10: 2, 2: 2}),
    (13, {}),
    (8, {2: 2, 9: 3, 10: 3, 3: 2, 5: 3}),
    (7, {3: 2, 6: 2, 1: 3}),
    (11, {1: 4, 6: 2, 4: 2}),
    (9, {2: 4, 8: 3, 10: 3, 5: 4}),
]

如果两个人有 1、2 或 3 个共同点,他们 可能 认识对方。如果他们有 4 个互惠生,他们 可能 彼此认识。我想处理这个列表,这样我就可以确定谁 might/probably 知道谁,结果输出如下:

Name: 1
    Might know: 4, 6, 7
    Probably knows: 11
Name: 2
    Might know: 5, 8, 10
    Probably knows: 9
Name: 3
    Might know: 4, 5, 6, 7, 8, 12
    Probably knows: 
Name: 4
    Might know: 1, 3, 6, 11, 12
    Probably knows: 
Name: 5
    Might know: 2, 3, 8, 10
    Probably knows: 9
Name: 6
    Might know: 1, 3, 4, 7, 11
    Probably knows: 
Name: 7
    Might know: 1, 3, 6
    Probably knows: 
Name: 8
    Might know: 2, 3, 5, 9, 10
    Probably knows: 
Name: 9
    Might know: 8, 10
    Probably knows: 2, 5
Name: 10
    Might know: 2, 5, 8, 9
    Probably knows: 
Name: 11
    Might know: 4, 6
    Probably knows: 1
Name: 12
    Might know: 3, 4
    Probably knows: 

这是我目前用来处理它的代码:

might = []
probably = []
for person in network:
    name = person[0]
    connections = person[1]
    for other_name, mutuals in connections.items():
        if mutuals > 3:
            probably.append(str(other_name))
        else:         
            might.append(str(other_name))

但我只得到了两个列表:

['3', '4', '7', '1', '11', '7', '6', '4', '3', '6', '1', '11', '12', '8', '10',
 '5', '3', '4', '5', '8', '12', '4', '7', '6', '2', '9', '8', '5', '3', '8',
 '10', '2', '2', '9', '10', '3', '5', '3', '6', '1', '6', '4', '8', '10']

['11', '9', '9', '1', '2', '5']

如何将这些与专有名称相关联?

你想要的输出本质上是一个字典,所以这样构建它是有意义的。每个键都是一个名字;每个值将是另一个字典,键为 'might' 和 'probably'。 (它的值都是列表。)

output = {}
for name, connections in network:
    # If we've not added this name yet, create a blank entry:
    if name not in output:
        output[name] = {'probably': [], 'might': []}
    
    # Now loop through the connected people and add to the correct list:
    for other_name, mutuals in connections.items():
        if mutuals > 3:
            output[name]['probably'].append(other_name)
        else:
            output[name]['might'].append(other_name)

此时,我们可以使用 Python 的 pprint 函数来检查我们是否走在正确的轨道上。 (对于像这样的嵌套结构,它比 print 更具可读性。)

from pprint import pprint
pprint(output)

输出

{1: {'might': [7, 6, 4], 'probably': [11]},
 2: {'might': [8, 10, 5], 'probably': [9]},
 3: {'might': [5, 8, 12, 4, 7, 6], 'probably': []},
 4: {'might': [3, 6, 1, 11, 12], 'probably': []},
 5: {'might': [3, 8, 10, 2], 'probably': [9]},
 6: {'might': [3, 4, 7, 1, 11], 'probably': []},
 7: {'might': [3, 6, 1], 'probably': []},
 8: {'might': [2, 9, 10, 3, 5], 'probably': []},
 9: {'might': [8, 10], 'probably': [2, 5]},
 10: {'might': [2, 9, 8, 5], 'probably': []},
 11: {'might': [6, 4], 'probably': [1]},
 12: {'might': [3, 4], 'probably': []},
 13: {'might': [], 'probably': []}}

(请注意,pprint 会自动对要显示的键进行排序:它们实际上并非按该顺序排列。)

现在我们需要做的就是将其格式化以供显示,我们可以随心所欲。到目前为止,我将名称保留为整数,以便我们可以正确地对它们进行排序(而不是让 112 之前结束,就像在对字符串进行排序时那样)。如果这些赋值看起来很复杂,请查看 str.join and list comprehensions. And you may or may not know about f-strings,它们也非常方便(并且不需要变量甚至是字符串!)

for name, contents in sorted(output.items()):
    print(f'Name: {name}')

    might = ', '.join([str(i) for i in sorted(contents['might'])])
    print(f'\tMight know: {might}')

    probably = ', '.join([str(i) for i in sorted(contents['probably'])])
    print(f'\tProbably knows: {probably}')

输出:

Name: 1
    Might know: 4, 6, 7
    Probably knows: 11
Name: 2
    Might know: 5, 8, 10
    Probably knows: 9
Name: 3
    Might know: 4, 5, 6, 7, 8, 12
    Probably knows: 
Name: 4
    Might know: 1, 3, 6, 11, 12
    Probably knows: 
Name: 5
    Might know: 2, 3, 8, 10
    Probably knows: 9
Name: 6
    Might know: 1, 3, 4, 7, 11
    Probably knows: 
Name: 7
    Might know: 1, 3, 6
    Probably knows: 
Name: 8
    Might know: 2, 3, 5, 9, 10
    Probably knows: 
Name: 9
    Might know: 8, 10
    Probably knows: 2, 5
Name: 10
    Might know: 2, 5, 8, 9
    Probably knows: 
Name: 11
    Might know: 4, 6
    Probably knows: 1
Name: 12
    Might know: 3, 4
    Probably knows: 
Name: 13
    Might know: 
    Probably knows: