Создать график из dicts python

#python

#python

Вопрос:

У меня есть dicts

 {'IMG_0003_1.tif': ['IMG_0004_1.tif', 'IMG_0005_1.tif'], 
'IMG_0004_1.tif': ['IMG_0005_1.tif', 'IMG_0003_1.tif'], 
'IMG_0005_1.tif': ['IMG_0004_1.tif', 'IMG_0006_1.tif'], 
'IMG_0006_1.tif': ['IMG_0007_1.tif', 'IMG_0005_1.tif'], 
'IMG_0007_1.tif': ['IMG_0006_1.tif', 'IMG_0008_1.tif'], 
'IMG_0008_1.tif': ['IMG_0009_1.tif', 'IMG_0007_1.tif'], 
'IMG_0009_1.tif': ['IMG_0008_1.tif', 'IMG_0010_1.tif'], 
'IMG_0010_1.tif': ['IMG_0009_1.tif', 'IMG_0011_1.tif'], 
'IMG_0011_1.tif': ['IMG_0012_1.tif', 'IMG_0013_1.tif'], 
'IMG_0012_1.tif': ['IMG_0011_1.tif', 'IMG_0013_1.tif'], 
'IMG_0013_1.tif': ['IMG_0014_1.tif', 'IMG_0012_1.tif'], 
'IMG_0014_1.tif': ['IMG_0013_1.tif', 'IMG_0015_1.tif'],
'IMG_0015_1.tif': ['IMG_0014_1.tif', 'IMG_0016_1.tif'], 
'IMG_0016_1.tif': ['IMG_0017_1.tif', 'IMG_0015_1.tif'], 
'IMG_0017_1.tif': ['IMG_0019_1.tif', 'IMG_0018_1.tif'], 
'IMG_0018_1.tif': ['IMG_0019_1.tif', 'IMG_0017_1.tif'],
'IMG_0019_1.tif': ['IMG_0018_1.tif', 'IMG_0017_1.tif'],
'IMG_0020_1.tif': ['IMG_0021_1.tif', 'IMG_0022_1.tif'],
'IMG_0021_1.tif': ['IMG_0020_1.tif', 'IMG_0022_1.tif'], 
'IMG_0022_1.tif': ['IMG_0023_1.tif', 'IMG_0021_1.tif'], 
'IMG_0023_1.tif': ['IMG_0022_1.tif', 'IMG_0021_1.tif']}
  

ключ —
значение изображения — список соседних изображений
Как создать график из этого dicts с помощью python networkx?
это будет использоваться для сшивания изображений

Ответ №1:

Используйте from_dict_of_lists:

 import networkx as nx

d = {'IMG_0003_1.tif': ['IMG_0004_1.tif', 'IMG_0005_1.tif'],
     'IMG_0004_1.tif': ['IMG_0005_1.tif', 'IMG_0003_1.tif'],
     'IMG_0005_1.tif': ['IMG_0004_1.tif', 'IMG_0006_1.tif'],
     'IMG_0006_1.tif': ['IMG_0007_1.tif', 'IMG_0005_1.tif'],
     'IMG_0007_1.tif': ['IMG_0006_1.tif', 'IMG_0008_1.tif'],
     'IMG_0008_1.tif': ['IMG_0009_1.tif', 'IMG_0007_1.tif'],
     'IMG_0009_1.tif': ['IMG_0008_1.tif', 'IMG_0010_1.tif'],
     'IMG_0010_1.tif': ['IMG_0009_1.tif', 'IMG_0011_1.tif'],
     'IMG_0011_1.tif': ['IMG_0012_1.tif', 'IMG_0013_1.tif'],
     'IMG_0012_1.tif': ['IMG_0011_1.tif', 'IMG_0013_1.tif'],
     'IMG_0013_1.tif': ['IMG_0014_1.tif', 'IMG_0012_1.tif'],
     'IMG_0014_1.tif': ['IMG_0013_1.tif', 'IMG_0015_1.tif'],
     'IMG_0015_1.tif': ['IMG_0014_1.tif', 'IMG_0016_1.tif'],
     'IMG_0016_1.tif': ['IMG_0017_1.tif', 'IMG_0015_1.tif'],
     'IMG_0017_1.tif': ['IMG_0019_1.tif', 'IMG_0018_1.tif'],
     'IMG_0018_1.tif': ['IMG_0019_1.tif', 'IMG_0017_1.tif'],
     'IMG_0019_1.tif': ['IMG_0018_1.tif', 'IMG_0017_1.tif'],
     'IMG_0020_1.tif': ['IMG_0021_1.tif', 'IMG_0022_1.tif'],
     'IMG_0021_1.tif': ['IMG_0020_1.tif', 'IMG_0022_1.tif'],
     'IMG_0022_1.tif': ['IMG_0023_1.tif', 'IMG_0021_1.tif'],
     'IMG_0023_1.tif': ['IMG_0022_1.tif', 'IMG_0021_1.tif']}

G = nx.from_dict_of_lists(d)

for edge in G.edges:
    print(edge)
  

Вывод (частичный)

 ('IMG_0003_1.tif', 'IMG_0004_1.tif')
('IMG_0003_1.tif', 'IMG_0005_1.tif')
('IMG_0004_1.tif', 'IMG_0005_1.tif')
('IMG_0005_1.tif', 'IMG_0006_1.tif')
('IMG_0006_1.tif', 'IMG_0007_1.tif')
('IMG_0007_1.tif', 'IMG_0008_1.tif')
('IMG_0008_1.tif', 'IMG_0009_1.tif')
('IMG_0009_1.tif', 'IMG_0010_1.tif')
('IMG_0010_1.tif', 'IMG_0011_1.tif')
('IMG_0011_1.tif', 'IMG_0012_1.tif')
('IMG_0011_1.tif', 'IMG_0013_1.tif')
  

Комментарии:

1. Хорошо! Спасибо!