python分布式邻居节点通讯_如何在python中找到节点间的邻居

该博客介绍了一个Python脚本,用于读取节点邻居列表并输出所有邻居的完整列表。通过NodeList类管理节点及其关系,添加节点对,并实现查找邻居的功能。示例数据展示了如何运行脚本并输出节点的邻居集合。
摘要由CSDN通过智能技术生成

#!/usr/bin/env python3

# nodes

#

# Read in lists of node neighbors, and output complete list of all neighbors

#

# nodeA nodeB

# nodeC nodeD

class NodeList:

def __init__(self):

self.nodes = {}

self.groups = 0

def add_pair(self, n1, n2): # add a pair of nodes

self.add_node(n1, n2) # they are neighbors

self.add_node(n2, n1) # to each other

def add_node(self, n1, n2):

if n1 not in self.nodes:

self.nodes[n1] = []

if n2 not in self.nodes[n1]: # add n2 only if not already a link

self.nodes[n1].append(n2)

self.groups += 1

def neighbors(self):

return list(map(lambda k: sorted(self.nodes[k]), sorted(self.nodes.keys())))

nodes = NodeList()

while True:

try:

line = input()

nums = [int(x) for x in line.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值