I have a Python dictionary that looks like this:
{‘Aitkin’: [‘Carlton’, ‘Cass’, ‘Crow Wing’, ‘Itasca’,
‘Kanabec’, ‘Mille Lacs’, ‘Pine’, ‘St. Louis’], ‘Anoka’: [‘Chisago’, ‘Hennepin’, ‘Isanti’, ‘Ramsey’, ‘Sherburne’,
‘Washington’], ‘Becker’: [‘Clay’, ‘Clearwater’, ‘Hubbard’,
‘Mahnomen’, ‘Norman’, ‘Otter Tail’, ‘Wadena’], ‘Beltrami’: [‘Cass’, ‘Clearwater’, ‘Hubbard’, ‘Itasca’, ‘Koochiching’,
‘Lake of the Woods’, ‘Marshall’, ‘Pennington’, ‘Roseau’], ‘Benton’: [‘Mille Lacs’, ‘Morrison’, ‘Sherburne’, ‘Stearns’], ‘Big Stone’: [‘Lac qui Parle’, ‘Stevens’, ‘Swift’, ‘Traverse’], ‘Blue Earth’: [‘Brown’, ‘Faribault’, ‘Le Sueur’, ‘Martin’,
‘Nicollet’, ‘Waseca’, ‘Watonwan’], ‘Brown’: [‘Blue Earth’,
‘Cottonwood’, ‘Nicollet’, ‘Redwood’, ‘Renville’, ‘Watonwan’], ‘Carlton’: [‘Aitkin’, ‘Pine’, ‘St. Louis’], ‘Carver’: [‘Hennepin’, ‘McLeod’, ‘Scott’, ‘Sibley’, ‘Wright’], ‘Cass’: [‘Aitkin’,
‘Beltrami’, ‘Crow Wing’, ‘Hubbard’, ‘Itasca’, ‘Morrison’,
‘Todd’, ‘Wadena’], ‘Chippewa’: [‘Kandiyohi’, ‘Lac qui Parle’,
‘Renville’, ‘Swift’, ‘Yellow Medicine’], ‘Chisago’: [‘Anoka’, ‘Isanti’, ‘Kanabec’, ‘Pine’, ‘Washington’], ‘Clay’: [‘Becker’, ‘Norman’, ‘Otter Tail’, ‘Wilkin’], ‘Clearwater’: [‘Becker’,
‘Beltrami’, ‘Hubbard’, ‘Mahnomen’, ‘Pennington’, ‘Polk’], ‘Cook’: [‘Lake’], ‘Cottonwood’: [‘Brown’, ‘Jackson’, ‘Murray’, ‘Nobles’, ‘Redwood’, ‘Watonwan’], ‘Crow Wing’: [‘Aitkin’, ‘Cass’, ‘Mille Lacs’, ‘Morrison’], ‘Dakota’: [‘Goodhue’, ‘Hennepin’, ‘Ramsey’, ‘Rice’, ‘Scott’, ‘Washington’], ‘Dodge’: [‘Goodhue’, ‘Mower’, ‘Olmsted’, ‘Rice’, ‘Steele’], ‘Douglas’: [‘Grant’, ‘Otter Tail’, ‘Pope’, ‘Stearns’, ‘Stevens’, ‘Todd’], ‘Faribault’: [‘Blue Earth’, ‘Freeborn’, ‘Martin’, ‘Waseca’], ‘Fillmore’: [‘Houston’, ‘Mower’, ‘Olmsted’, ‘Winona’], ‘Freeborn’: [‘Faribault’, ‘Mower’, ‘Steele’, ‘Waseca’], ‘Goodhue’: [‘Dakota’, ‘Dodge’, ‘Olmsted’, ‘Rice’, ‘Wabasha’], ‘Grant’: [‘Douglas’, ‘Otter Tail’, ‘Pope’, ‘Stevens’, ‘Traverse’, ‘Wilkin’], ‘Hennepin’: [‘Anoka’, ‘Carver’,
‘Dakota’, ‘Ramsey’, ‘Scott’, ‘Sherburne’, ‘Wright’], ‘Houston’: [‘Fillmore’, ‘Winona’], ‘Hubbard’: [‘Becker’, ‘Beltrami’, ‘Cass’, ‘Clearwater’, ‘Wadena’], ‘Isanti’: [‘Anoka’, ‘Chisago’, ‘Kanabec’, ‘Mille Lacs’, ‘Pine’, ‘Sherburne’], ‘Itasca’: [‘Aitkin’, ‘Beltrami’, ‘Cass’, ‘Koochiching’, ‘St. Louis’], ‘Jackson’: [‘Cottonwood’, ‘Martin’, ‘Nobles’, ‘Watonwan’], ‘Kanabec’: [‘Aitkin’, ‘Chisago’, ‘Isanti’, ‘Mille Lacs’, ‘Pine’], ‘Kandiyohi’: [‘Chippewa’, ‘Meeker’, ‘Pope’, ‘Renville’, ‘Stearns’, ‘Swift’], ‘Kittson’: [‘Marshall’, ‘Roseau’], ‘Koochiching’: [‘Beltrami’, ‘Itasca’, ‘Lake of the Woods’, ‘St. Louis’], ‘Lac qui Parle’: [‘Big Stone’, ‘Chippewa’, ‘Swift’, ‘Yellow Medicine’], ‘Lake’: [‘Cook’, ‘St. Louis’], ‘Lake of the Woods’: [‘Beltrami’, ‘Koochiching’, ‘Roseau’], ‘Le Sueur’: [‘Blue Earth’, ‘Nicollet’, ‘Rice’, ‘Scott’, ‘Sibley’, ‘Waseca’], ‘Lincoln’: [‘Lyon’, ‘Pipestone’, ‘Yellow Medicine’], ‘Lyon’: [‘Lincoln’, ‘Murray’, ‘Pipestone’, ‘Redwood’, ‘Yellow Medicine’], ‘Mahnomen’: [‘Becker’, ‘Clearwater’, ‘Norman’, ‘Polk’], ‘Marshall’: [‘Beltrami’, ‘Kittson’, ‘Pennington’, ‘Polk’, ‘Roseau’], ‘Martin’: [‘Blue Earth’, ‘Faribault’, ‘Jackson’, ‘Watonwan’], ‘McLeod’: [‘Carver’, ‘Meeker’, ‘Renville’, ‘Sibley’, ‘Wright’], ‘Meeker’: [‘Kandiyohi’, ‘McLeod’, ‘Renville’, ‘Stearns’, ‘Wright’], ‘Mille Lacs’: [‘Aitkin’, ‘Benton’, ‘Crow Wing’, ‘Isanti’,
‘Kanabec’, ‘Morrison’, ‘Sherburne’], ‘Morrison’: [‘Benton’, ‘Cass’, ‘Crow Wing’, ‘Mille Lacs’, ‘Stearns’, ‘Todd’], ‘Mower’: [‘Dodge’, ‘Fillmore’, ‘Freeborn’, ‘Olmsted’, ‘Steele’], ‘Murray’: [‘Cottonwood’, ‘Lyon’, ‘Nobles’, ‘Pipestone’, ‘Redwood’, ‘Rock’], ‘Nicollet’: [‘Blue Earth’, ‘Brown’, ‘Le Sueur’, ‘Renville’, ‘Sibley’], ‘Nobles’: [‘Cottonwood’, ‘Jackson’, ‘Murray’, ‘Rock’], ‘Norman’: [‘Becker’, ‘Clay’, ‘Mahnomen’, ‘Polk’], ‘Olmsted’: [‘Dodge’, ‘Fillmore’, ‘Goodhue’, ‘Mower’, ‘Wabasha’, ‘Winona’], ‘Otter Tail’: [‘Becker’, ‘Clay’, ‘Douglas’, ‘Grant’, ‘Wadena’, ‘Wilkin’], ‘Pennington’: [‘Beltrami’, ‘Clearwater’, ‘Marshall’, ‘Polk’, ‘Red Lake’], ‘Pine’: [‘Aitkin’, ‘Carlton’, ‘Chisago’, ‘Isanti’, ‘Kanabec’], ‘Pipestone’: [‘Lincoln’, ‘Lyon’, ‘Murray’, ‘Rock’], ‘Polk’: [‘Clearwater’, ‘Mahnomen’, ‘Marshall’, ‘Norman’,
‘Pennington’, ‘Red Lake’], ‘Pope’: [‘Douglas’, ‘Grant’, ‘Kandiyohi’, ‘Stearns’, ‘Stevens’, ‘Swift’], ‘Ramsey’: [‘Anoka’, ‘Dakota’, ‘Hennepin’, ‘Washington’], ‘Red Lake’: [‘Pennington’, ‘Polk’], ‘Redwood’: [‘Brown’, ‘Cottonwood’, ‘Lyon’, ‘Murray’,
‘Renville’, ‘Yellow Medicine’], ‘Renville’: [‘Brown’, ‘Chippewa’, ‘Kandiyohi’, ‘McLeod’, ‘Meeker’, ‘Nicollet’, ‘Redwood’,
‘Sibley’, ‘Yellow Medicine’], ‘Rice’: [‘Dakota’, ‘Dodge’,
‘Goodhue’, ‘Le Sueur’, ‘Scott’, ‘Steele’, ‘Waseca’], ‘Rock’: [‘Murray’, ‘Nobles’, ‘Pipestone’], ‘Roseau’: [‘Beltrami’, ‘Kittson’, ‘Lake of the Woods’, ‘Marshall’], ‘Scott’: [‘Carver’, ‘Dakota’, ‘Hennepin’, ‘Le Sueur’, ‘Rice’, ‘Sibley’], ‘Sherburne’: [‘Anoka’,
‘Benton’, ‘Hennepin’, ‘Isanti’, ‘Mille Lacs’, ‘Stearns’,
‘Wright’], ‘Sibley’: [‘Carver’, ‘Le Sueur’, ‘McLeod’, ‘Nicollet’, ‘Renville’, ‘Scott’], ‘St. Louis’: [‘Aitkin’, ‘Carlton’, ‘Itasca’, ‘Koochiching’, ‘Lake’], ‘Stearns’: [‘Benton’, ‘Douglas’,
‘Kandiyohi’, ‘Meeker’, ‘Morrison’, ‘Pope’, ‘Sherburne’,
‘Todd’, ‘Wright’], ‘Steele’: [‘Dodge’, ‘Freeborn’, ‘Mower’, ‘Rice’, ‘Waseca’], ‘Stevens’: [‘Big Stone’, ‘Douglas’, ‘Grant’, ‘Pope’, ‘Swift’, ‘Traverse’], ‘Swift’: [‘Big Stone’, ‘Chippewa’,
‘Kandiyohi’, ‘Lac qui Parle’, ‘Pope’, ‘Stevens’], ‘Todd’: [‘Cass’, ‘Douglas’, ‘Morrison’, ‘Otter Tail’, ‘Stearns’, ‘Wadena’], ‘Traverse’: [‘Big Stone’, ‘Grant’, ‘Stevens’, ‘Wilkin’], ‘Wabasha’: [‘Goodhue’, ‘Olmsted’, ‘Winona’], ‘Wadena’: [‘Becker’, ‘Cass’, ‘Hubbard’, ‘Otter Tail’, ‘Todd’], ‘Waseca’: [‘Blue Earth’,
‘Faribault’, ‘Freeborn’, ‘Le Sueur’, ‘Rice’, ‘Steele’], ‘Washington’: [‘Anoka’, ‘Chisago’, ‘Dakota’, ‘Ramsey’], ‘Watonwan’: [‘Blue Earth’, ‘Brown’, ‘Cottonwood’, ‘Jackson’, ‘Martin’], ‘Wilkin’: [‘Clay’, ‘Grant’, ‘Otter Tail’, ‘Traverse’], ‘Winona’: [‘Fillmore’, ‘Houston’, ‘Olmsted’, ‘Wabasha’], ‘Wright’: [‘Carver’, ‘Hennepin’, ‘McLeod’, ‘Meeker’, ‘Sherburne’, ‘Stearns’], ‘Yellow Medicine’: [‘Chippewa’, ‘Lac qui Parle’, ‘Lincoln’, ‘Lyon’, ‘Redwood’,
‘Renville’]}
The keys in the dictionary represent the nodes, while the values(lists) represent nodes of neighbors of the key. This is an undirected unweighted graph.
Is there some function to implement this in networkx
or some network analysis or graph-related libraries?
Advertisement
Answer
You can use the nx.Graph
constructor — it will add additional nodes if they don’t appear as keys in the original dictionary (data
represents the dictionary in the original question):
import networkx as nx graph = nx.Graph(data)