10월, 2016의 게시물 표시

python2 class 리스트,매트릭스 멤버간 합

class aaaa:         def __init__(self,x):                 self.x =x         def __myadd__(self,other):                 z = self.x+other                 return z         def __addlst__(self,lst):                 for i in range(0,len(self.x)):                         self.x[i] = self.x[i] + lst[i]                 return self.x         def __addmatrix__(self,mat):                 for i in range(0,len(self.x)):                         for j in range(0,len(self.x[i])):                       ...

python3 wordcount txt파일 읽어서 단어개수 출력

이미지
#-*- coding: utf-8 -*- import time import re   regex = re.compile('[a-z]+|[A-Z]+|[^0-9]') t1 =time.time()   f = open("읽고싶은 텍스트파일.txt",'r') text=f.read()   wordss=text.split() words=regex.findall(str(wordss)) word_counts=dict()     for word in words: word_counts[word]=word_counts.get(word,0)+1   for word,count in word_counts.items(): print(word,count)   f.close() t2 =time.time() print(t2-t1) 정규식과 딕셔너리 자료형을 이용하여 영어단어를 찾아 개수를 출력해주는 코드. 실행시간도 출력해준다. 100MB 정도 되는 텍스트 파일을 우분투 환경에서 실행 시킨 결과 이미지 대략 40초 정도 걸렸다. 보다 정교하게 걸러내기 위해서는 코드를 손봐야한다.