2011-04-11 5 views
0

이 코드가 있습니다. 이 프로그램은 데이터 라인튜플 값을 늘리고 파이썬의 루프에서 문자열을 검색하는 방법

0,1,0,0,0,0,0,0,0,1,1,No,cluster3 

를 예 내가 얼마나 많은 클러스터를 검출하는 사전을 생성 한 판독 로선

arfffile = [] 

inputed = raw_input("Enter Evaluation for name including file extension...") 

reader = open(inputed, 'r') 

verses = [] 

for line in reader: 
    verses.append(line) 

for line in verses: 
    if line.split('@') == "@": 
     verses.pop(line) 


numclusters = int(raw_input("Enter the number of clusters")) 

clusters = {} 

for i in range(1,numclusters+1): 
    clusters["cluster"+str(i)] = 0 



print clusters 
# If verse belongs to a cluster, increment the cluster count by one in the dictionary value. 
for verse in verses: 
    for k in clusters: 
     if k in verse: 
      clusters[k] += 1 
     else: 
      print "not in" 

print clusters 

yeslist = [] 

for verse in verses: 
    for k in clusters: 
     if k not in yeslist: 
      yeslist.append((k,0)) 
     elif k in yeslist: 
      print "already in" + k 


for verse in verses: 
    for k in clusters: 
     if k in verse and "Yes" in verse: 
      yeslist.append(yeslist.index(k), +1) 


    # iterate through dictionary and iterate through the lines 
    # need to read in file line by line, 



    # if "yes" and cluster x increment cluster 
    # need to work out percentage of possitive verses in each cluster. 

arff 파일의 예

@relation tester999.arff_clustered 

@attribute Instance_number numeric 
@attribute allah numeric 
@attribute day numeric 
@attribute lord numeric 
@attribute people numeric 
@attribute earth numeric 
@attribute men numeric 
@attribute truth numeric 
@attribute verily numeric 
@attribute chapter numeric 
@attribute verse numeric 
@attribute CLASS {Yes,No} 
@attribute Cluster {cluster1,cluster2,cluster3} 

@data 
0,1,0,0,0,0,0,0,0,1,1,No,cluster3 
1,1,0,0,0,0,0,0,0,1,2,No,cluster3 
2,0,0,0,0,0,0,0,0,1,3,No,cluster3 
3,0,1,0,0,0,1,0,0,1,4,No,cluster3 
4,0,0,0,0,0,0,0,0,1,5,No,cluster3 
5,0,0,0,0,0,0,0,0,1,6,No,cluster3 
6,0,0,0,0,0,0,0,0,1,7,No,cluster3 
7,0,0,0,0,0,0,0,0,2,1,No,cluster3 
8,1,0,0,0,0,0,0,0,2,2,No,cluster3 
9,0,0,0,0,0,0,0,0,2,3,No,cluster3 
10,0,0,0,0,0,0,0,0,2,4,No,cluster3 
11,0,0,1,0,0,0,0,0,2,5,No,cluster2 

인 데이터 파일에 있습니다. 이 예제에는 cluster1 cluster2와 cluster3이 있습니다. 그런 다음 코드는 사전 "클러스터"에 문자열로 표시된 키 값으로 각 클러스터를 추가합니다.
그런 다음 모든 절에 대해 반복하고 각 행을 계산하여 어느 클러스터에 속하는지 확인합니다.

다음 단계는 각 클러스터에 대해 "예"라는 줄이 몇 번 있는지 계산하는 것입니다. 그래서 거기에 "예"데이터의 각 줄에 문자열에 "예"라고 말하면, 코드는이 발생 수를 계산할 수 있어야합니다.

가 지금까지 행한 코드 여기

for verse in verses: 
     for k in clusters: 
      if k in verse and "Yes" in verse: 
       yeslist.append(yeslist.index(k), +1) 

basicaly이 같은 값 "yeslist"라는 튜플들의 목록을 작성하는`난 [(클러스터 1, 0), (cluster2 3)]

각 행 (문자열로 표시)에 "예"가 있는지 확인하고, 어느 클러스터에 속해 있는지 확인한 다음 해당 튜플 값을 1 씩 증가시킵니다.

나는 이것을하는 방법의 논리를 생각하는 데 어려움을 겪고 있습니다 ... 누구든지 도울 수 있습니까?

감사합니다.

+1

과 질문의 짧은 변종이다 : 당신이 정말로 튜플의 목록을 원하는 경우

cluster_count : keys = cluster#, values = count yes_count : keys = cluster#, values = #yes 

? –

+0

튜플을 변경할 수 없다고 확신합니다. – DTing

답변

1
import collections 

inputed = raw_input("Enter Evaluation for name including file extension...") 

reader = open(inputed, 'r') 

verses = [ line.strip() for line in reader.readlines() if line[0] != '@' ] 

reader.close() 

cluster_count = collections.defaultdict(int) 
yes_count = collections.defaultdict(int) 

verse_infos = [ (split_verse[-1],split_verse[-2]) for split_verse \ 
       in verses.split(",") ] 

for verse in verse_infos: 
    cluster_count[verse[0]]+=1 
    if verse[1] == 'yes': 
     yes_count[verse[0]]+=1 

당신은 두 개의 사전으로 끝날 :

yes_tuples = (x for x in sorted(yes_count.iteritems())) 
관련 문제