毕业论文中用于计算联合国会员国间在联合国大会上的投票一致性(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. 微信小程序-自定义tabbar配置及注意事项

    1.选中要创建tabbar组件的目录,右键选定新建Componen 2.然后编写wxml代码和wxss样式 <cover-view class="tab-bar"> & ...

  2. nacos 1.4.2 建立集群,公司启动linux服务器常用命令

    2022-7-29 编写micro自动处理脚本,并加入了守护精灵进程 先启动prop1 服务器(xx.1xx.165.186) 再启动prop2服务器 (xx.1xx.174.173) ####### ...

  3. python retry装饰器

    from functools import wraps import time # def retry(retry_time, retry_on_result, time_wait): # def t ...

  4. file类型的input框获取文件

  5. R6-1 输入年份和天数,输出对应的年、月、日

    R6-1 输入年份和天数,输出对应的年.月.日 分数 15 全屏浏览题目 切换布局 作者 张泳 单位 浙大城市学院 要求定义和调用函数month_day ( year, yeardy, *****pm ...

  6. vs2019 文件读取操作

    1 #include<stdio.h> 2 #define INF 10000000 3 int main() 4 { 5 FILE* fin , * fout ; 6 errno_t a ...

  7. call与apply的区别与共同点

    call与apply区别 共同点:都会立即执行函数 call:是以逗号为间隔,传递多个参数 apply:是以数组来进行传递多个参数

  8. redis过期事件监听

    1 修改 redis.conf配置文件: K Keyspace events, published with keyspace@ prefix事件 E Keyevent events, publish ...

  9. 类Class

    C# 类(Class) 当你定义一个类时,你定义了一个数据类型的蓝图.这实际上并没有定义任何的数据,但它定义了类的名称意味着什么,也就是说,类的对象由什么组成及在这个对象上可执行什么操作.对象是类的实 ...

  10. DB2日常维护操作

    一. DB2日常维护操作 1.数据库的启动.停止.激活 db2 list active databases db2 active db 数据库名 db2start --启动 db2stop [forc ...