连接具有共同点的线
我有这样的行列表:
Lines = ['1', '2', '3', '4', '5', '6', '7', '8']
每行有两个点I和J:
LinesDetail = {
'1': {
'I': '100',
'J': '101'},
'2': {
'I': '101',
'J': '102'},
'3': {
'I': '256',
'J': '257'},
'4': {
'I': '257',
'J': '258'},
'5': {
'I': '258',
'J': '259'},
'6': {
'I': '304',
'J': '305'},
'7': {
'I': '305',
'J': '306'},
'8': {
'I': '102',
'J': '103'}}
如您在图片中所见,其中一些行有共同点 因此,它们相互连接,我需要知道哪些线相互连接。
我在循环时尝试过,但我没有解决此类问题的基本思想。
,结果是:
result = [["1","2","8"],["3","4","5"],["6","7"]]
所有行都是垂直的
I have a list of lines like this:
Lines = ['1', '2', '3', '4', '5', '6', '7', '8']
each line has two points I and J:
LinesDetail = {
'1': {
'I': '100',
'J': '101'},
'2': {
'I': '101',
'J': '102'},
'3': {
'I': '256',
'J': '257'},
'4': {
'I': '257',
'J': '258'},
'5': {
'I': '258',
'J': '259'},
'6': {
'I': '304',
'J': '305'},
'7': {
'I': '305',
'J': '306'},
'8': {
'I': '102',
'J': '103'}}
As you see in the picture, some of these lines have mutual points
so they are connected to each other and I need to know which lines are connected to each other.
I tried while loop but I don't have the basic idea of how to solve this kind of problems.
and the result would be:
result = [["1","2","8"],["3","4","5"],["6","7"]]
All Lines Are Vertical
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是查找连接组件的图形问题。一个可能的解释是外键是标签,内部字典是边缘(内部dict值是节点)。如果依赖关系不是问题,则Python具有涉及图形的不错的API 。具体而言,可以使用UnionFind数据结构来查找不相交的子集。
This is a graph problem of finding connected components. A possible interpretation is that the outer keys are labels and the inner dictionaries are the edges (and inner dict values are the nodes). If dependency is not an issue, Python has a nice API
networkx
that deals with graphs. Specifically, one can use the UnionFind data structure to find the disjoint subsets.这不是最佳的解决方案,而是将其放在那里以来,因为我一直在
输出它:
Not the most optimal solution, but putting it out there since I worked on it
output: