毕业论文中用于计算联合国会员国间在联合国大会上的投票一致性(IVC)

import pandas as pd
import sqlite3
import networkx as nx
import time
import numpy as np
def gen_dict(sql):#建立投票数据字典
votedict={}
votedict['all']={}
for c in cntylist:
votedict['all'][c]={}
votedict['all'][c]['yes']=[]
votedict['all'][c]['noyes']=[]
votedict['all'][c]['qq']=[]
votedict['all'][c]['no']=[]
votedict['all'][c]['all']=[]
for y in [1992,2001,2010]:
votedict[y]={}
for c in cntylist:
votedict[y][c]={}
votedict[y][c]['yes']=[]
votedict[y][c]['noyes']=[]
votedict[y][c]['qq']=[]
votedict[y][c]['no']=[]
votedict[y][c]['all']=[]
for c in cntylist:
c1=c
r1sql='select rcid, Country, vote, year from data where vote<4 and Country= \'%s\'' % c1 + sql
rec1=pd.read_sql(r1sql ,conn)
yeslist=rec1[rec1['vote']==1]['rcid'].tolist()
noyeslist=rec1[(rec1['vote']==2)|(rec1['vote']==3)]['rcid'].tolist()
nolist=rec1[rec1['vote']==3]['rcid'].tolist()
qqlist=rec1[rec1['vote']==2]['rcid'].tolist()
votedict['all'][c]['yes']=yeslist
votedict['all'][c]['noyes']=noyeslist
votedict['all'][c]['qq']=qqlist
votedict['all'][c]['no']=nolist
votedict['all'][c]['all']=rec1['rcid'].tolist()
for y in [1992,2001,2010]:
for c in cntylist:
c1=c
yearsql='and year>= %d and year< %d'%(y,y+8)
r1sql='select rcid, Country, vote, year from data where vote<4 and Country= \'%s\'' % c1+yearsql+sql
rec1=pd.read_sql(r1sql ,conn)
yeslist=rec1[rec1['vote']==1]['rcid'].tolist()
noyeslist=rec1[(rec1['vote']==2)|(rec1['vote']==3)]['rcid'].tolist()
nolist=rec1[rec1['vote']==3]['rcid'].tolist()
qqlist=rec1[rec1['vote']==2]['rcid'].tolist()
votedict[y][c]['yes']=yeslist
votedict[y][c]['noyes']=noyeslist
votedict[y][c]['qq']=qqlist
votedict[y][c]['no']=nolist
votedict[y][c]['all']=rec1['rcid'].tolist()
print(sql,y,'生成该年份数据')
print(sql,'数据字典已经生成')
return votedict
def ivc_gen_new(thedict,c1,c2):#基于字典计算两国间IVC
c1yes= thedict[c1]['yes']
c1no= thedict[c1]['no']
c1noyes=thedict[c1]['noyes']
c1qq=thedict[c1]['qq']
c2yes= thedict[c2]['yes']
c2no=thedict[c2]['no']
c2noyes=thedict[c2]['noyes']
c2qq=thedict[c2]['qq']
c1all=thedict[c1]['all']
c2all=thedict[c2]['all']
flist=[x for x in c1yes if x in c2yes]+[x for x in c1qq if x in c2qq]+[x for x in c1no if x in c2no]
glist=[x for x in c1qq if x in c2yes+c2no]+[x for x in c2qq if x in c1yes+c1no]
f=len(set(flist))
g=len(set(glist))
t=len(set([c for c in c1all if c in c2all]))
try:
ivc=(f+0.5*g)/t
except:
ivc=0
return ivc

IVC方法参考

Turner J. Party and constituency: Pressures on congress[M]. Baltimore: The Johns Hopkins Press, 1952.

python投票一致性指数(IVC)实现代码的更多相关文章

  1. Python实现各种排序算法的代码示例总结

    Python实现各种排序算法的代码示例总结 作者:Donald Knuth 字体:[增加 减小] 类型:转载 时间:2015-12-11我要评论 这篇文章主要介绍了Python实现各种排序算法的代码示 ...

  2. 【原创】用Python爬取LeetCode的AC代码到Github

    在leetCode写了105道题高调膜科,考虑搬迁到自己的GitHub上,做成一个解题题库,面试的时候也可以秀一个 但是!但是! leetCode在线IDE的功能不要太舒服,我直接线上A了不少题,本地 ...

  3. Python绘制3d螺旋曲线图实例代码

    Axes3D.plot(xs, ys, *args, **kwargs) 绘制2D或3D数据 参数 描述 xs, ys X轴,Y轴坐标定点 zs Z值,每一个点的值都是1 zdir 绘制2D集合时使用 ...

  4. Python爬虫 - 爬取百度html代码前200行

    Python爬虫 - 爬取百度html代码前200行 - 改进版,  增加了对字符串的.strip()处理 源代码如下: # 改进版, 增加了 .strip()方法的使用 # coding=utf-8 ...

  5. 软件测试自动化…python学习到什么程度?代码好不好学!

    软件测试自动化…python学习到什么程度?代码好不好学! 如下:

  6. python 类继承演示范例的代码

    把做工程过程重要的代码片段备份一次,下面的资料是关于python 类继承演示范例的代码. # a simple example of a class inheritance # tested with ...

  7. 震惊!!!python可以用中文来写代码

    python可以用中文来写代码 说明: 偶尔间试了一下,python可以用中文来写代码,除了一些python内置函数,和运算符不能用中文外,其它的比如新定义的类名.函数名.变量名,甚至是函数间传的参数 ...

  8. python爬取网页的通用代码框架

    python爬取网页的通用代码框架: def getHTMLText(url):#参数code缺省值为‘utf-8’(编码方式) try: r=requests.get(url,timeout=30) ...

  9. Python 工匠:编写条件分支代码的技巧

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由鹅厂优文发表于云+社区专栏 作者:朱雷 | 腾讯IEG高级工程师 『Python 工匠』是什么? 我一直觉得编程某种意义是一门『手艺』 ...

  10. Python解决八皇后问题的代码【解读】

    八皇后问题 来自于西方象棋(现在叫 国际象棋,英文chess),详情可见百度百科. 在西方象棋中,有一种叫做皇后的棋子,在棋盘上,如果双方的皇后在同一行.同一列或同一斜线上,就会互相攻击. 八皇后问题 ...

随机推荐

  1. 在axios拦截器js文件中无法使用vue-router问题

    如果在axios设置拦截器的js文件或者说在vue-router路由配置文件,配置好的路由文件以外的js文件中使用 import {useRouter} from 'vue-router' 系统不会默 ...

  2. Delphi 从字符串中提取数字

    function GetNumberFromStr(strIn: string; sFlag: string): string; var i: Integer; tempStr: string; be ...

  3. shell - scriptfifo

    shell1: mkfifo xxfifo script -f xxfifo cmd cmd exit   -----主动退出 shell2: cat xxfifo 可以看到shell1的所有操作. ...

  4. js常用代码整理2

    引用js <script type="text/javascript" src="js/jquery-1.11.2.min.js"></scr ...

  5. nginx配置根据url的参数值进行转发

    server { listen 8081; location / { set $tag ""; set $cs "/index/test/test"; prox ...

  6. MVC页面加载速度优化小记

     前言: 最近做一个地图展示页面,业务初期没什么问题,运行一阵后报错: Error during serialization or deserialization using the JSON Jav ...

  7. studiostyl.es网站scheme文件无法下载

    原因网站引用Google的服务 解决方法:查看网页源码.选择选取页面中的元素,选择download按钮,如下图所示 downloadExisting后面的数字代表主题的ID. 在网站中输入https: ...

  8. 使用 DataType 特性应用于 Date 和 Price 属性

    [Display(Name = "Release Date")] [DataType(DataType.Date)] public DateTime ReleaseDate { g ...

  9. WPF侧边导航栏实现

    一.先看效果 1 添加Nuget库 站长使用.Net Core 3.1创建的WPF工程,创建"DropDownMenu"解决方案后,需要添加两个Nuget库:MaterialDes ...

  10. docker实战(8)使用docker-compose快速搭建zookeeper集群

    镜像下载 docker pull zookeeper 复制 zookeeper 集群的搭建 创建名为docker-compose.yml的文件 输入以下内容 version: '2' services ...