Created
April 29, 2018 19:20
-
-
Save samerlahoud/123342a028abc22ac4f086e517ade810 to your computer and use it in GitHub Desktop.
Coalitions in the Lebanese Parliamentary Elections 2018
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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