Skip to content

Instantly share code, notes, and snippets.

@samerlahoud
Created April 29, 2018 19:20
Show Gist options
  • Save samerlahoud/123342a028abc22ac4f086e517ade810 to your computer and use it in GitHub Desktop.
Save samerlahoud/123342a028abc22ac4f086e517ade810 to your computer and use it in GitHub Desktop.
Coalitions in the Lebanese Parliamentary Elections 2018
#!/usr/bin/python
import networkx as nx
import json
coalition_graph=nx.Graph()
with open("./coalitions.txt",'r') as coalition_file:
for line in coalition_file:
party_list = line.rstrip('\n').split(' - ')
for i in range(0,len(party_list)):
for j in range(i+1,len(party_list)):
if coalition_graph.get_edge_data(party_list[i], party_list[j], default=0):
coalition_graph[party_list[i]][party_list[j]]['value'] = \
coalition_graph[party_list[i]][party_list[j]]['value'] + 1
else:
coalition_graph.add_edge(party_list[i], party_list[j], value = 1)
for n,deg in coalition_graph.degree():
coalition_graph.node[n]['degree'] = deg
for n in coalition_graph:
coalition_graph.node[n]['name'] = n
coalition_graph = nx.convert_node_labels_to_integers(coalition_graph)
data = nx.node_link_data(coalition_graph)
with open('coalition_graph.json', 'w') as f:
json.dump(data, f, indent=4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment