Skip to content

Instantly share code, notes, and snippets.

@jazzido
Created October 22, 2010 00:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jazzido/639693 to your computer and use it in GitHub Desktop.
Save jazzido/639693 to your computer and use it in GitHub Desktop.
# no me digan nada, es horrible.
import csv
import json
rows = list(csv.reader(open('votos2010.csv').readlines(), delimiter=";"))[1:]
diputados = map(lambda x: x[0], rows)
partidos = list(set(map(lambda x: x[1], rows)))
def comp(vd1, vd2):
""" Compara similaridad entre dos diputados segun sus votaciones.
Son mas similares por haber estado ambos ausentes en la votacion? (eg, no dieron quorum?) """
q = 0
for i in range(len(vd1)):
if vd1[i] == vd2[i]: q += 1
return q
arcs = dict()
nodes = list()
for i in range(len(rows)):
d1 = rows[i]
for j in range(i+1, len(rows)):
d2 = rows[j]
q = comp(d1[4:], d2[4:])
if q > 0: arcs[(i,j,)] = q
nodes.append({'nodeName': d1[0], 'group': partidos.index(d1[1])})
rv = { 'nodes': nodes,
'links': [{'source': k[0], 'target': k[1], 'value': v} for k,v in arcs.items()]
}
print json.dumps(rv, indent=2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment