- # -*-coding:utf-8 -*-
- # by csanmum
- # 程序思路:先构建一个方阵,再更改方阵中的元素,最后写入文本输出
- # 存在问题:当 line = 2k时,很久都不出结果。
- # 估计问题存在点:三个for循环(for循环本身很慢)
- import time
- start_time = time.clock()
- import os,re,os.path,glob,networkx,numpy
- os.chdir("D:\\\\folder\\\\")
- file_list = glob.glob("*")
- def get_content(filename):
- content = open(filename).readlines()
- content_new = []
- for e in content:
- e = e.rstrip("\\n")
- content_new.append(e)
- return content_new
- con_list = get_content("D:\\\\name.txt")
- print len(con_list) # 2000
- #create an square matrix, the element in diag are 1,others are 0;
- my_matrix = numpy.identity(len(con_list))
- for i in range(0,len(con_list)-1):
- e = con_list[i].capitalize()
- if e in file_list: # 2w file
- pcc_list = get_content(e)
- for j in range(i+1,len(con_list)):
- for ea in pcc_list:
- eb = ea.split("\\t")
- if con_list[j].capitalize() == eb[0]:
- my_matrix[i,j] = my_matrix[j,i] = eb[2]
- else:
- print "do not find this file: ",e
- numpy.savetxt("E:\\\\matrix.txt",my_matrix,"%5.3f")
- end_time = time.clock()
- print "cost time: ",(end_time - start_time)
- #该片段来自于http://www.codesnippet.cn/detail/030920135578.html
来源: http://www.codesnippet.cn/detail/030920135578.html