-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcal.py
52 lines (43 loc) · 1.29 KB
/
cal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# -*- coding: UTF-8 -*-
from numpy import *
import operator
import sys
import re
from os import listdir
def loadGraphDict(fileName):
dataDict = {}
fx = open(fileName)
for line in fx.readlines():
lineArr = re.split(' |,|\t',line.strip())
lenth=len(lineArr)
if(lenth<2):
break;
if(lineArr[0] not in dataDict.keys()):
dataDict[str(lineArr[0])]=[str(lineArr[1])]
if(lineArr[1] not in dataDict[str(lineArr[0])]):
dataDict[str(lineArr[0])].append(str(lineArr[1]))
if(lineArr[1] not in dataDict.keys()):
dataDict[str(lineArr[1])]=[str(lineArr[0])]
if(lineArr[0] not in dataDict[str(lineArr[1])]):
dataDict[str(lineArr[1])].append(str(lineArr[0]))
return dataDict
def start(filename):
xDict=loadGraphDict(filename)
i=0
sumdu=0.0
for node in xDict.keys():
sumdu+=len(xDict[node])
i+=1
print("items="),i
print("average degree="),float(sumdu/i)
fp=open('data/xDict.txt','w')
for li,lis in xDict.items():
fp.write('%s '%str(li))
for h in lis:
fp.write('%s '%str(h))
fp.write('\n')
fp.close()
return i,float(sumdu/i),xDict
if __name__=="__main__":
filename=sys.argv[1]
start(filename)