如何在python中增加元组值并在循环中搜索字符串

时间:2011-04-11 17:42:48

标签: python dictionary tuples

我有这个代码。

 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

目前,程序读入数据行,例如

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

我创建了一个字典,用于检测数据文件中有多少个集群。在此示例中,有3. cluster1 cluster2和cluster3。然后代码将每个簇附加为在字典“簇”中表示为字符串的键值 然后我遍历所有经文并计算每一行以查看它属于哪个集群。

我的下一步是尝试为每个群集计算其中出现“是”的行的次数。所以说数据中每行的字符串中有10行“是”,代码应该能够计算出这个行的出现次数。

到目前为止,我所做的代码就在这里

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

我基本上创建一个名为“yeslist”的元组列表,其值为[[cluster1,0],(cluster2,3)]

因此,对于每一行(表示为字符串),检查其中是否存在“是”,如果检查它属于哪个群集,则将该元组值递增1。

我很难想到如何做到这一点的逻辑......任何人都可以帮忙吗?

感谢。

1 个答案:

答案 0 :(得分: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

你最终得到两个词典:

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

如果你真的想要一个元组列表:

yes_tuples = ( x for x in sorted(yes_count.iteritems()) )