我有一个包含这些值的诗句字典:
{cluster1: 0, cluster2: 0, cluster3: 0}
我有一个已读入的数据文件,并且文件中的每一行都在这样的字典中表示为字符串。
[ "0,1,0,0,0,0,0,0,0,1,1,No,cluster3"," 0,1,0,0,1,0,0,0,0,1,1,No,cluster2" ]
我希望能够,对于数据文件中的每一行(表示为列表中的字符串),通过字典并比较键值,例如。cluster1 以查看它是否包含子字符串“cluster1”2 或 3。然后相应地更新字典中的值。因此,该程序的目的是计算每个集群的出现次数,并将其表示为一个字典,其中包含集群编号和每个集群的相应计数。
我只是不确定执行此操作的语法。到目前为止,这是我的循环:
for verse in verses:
for clusters[Key] in clusters:
if clusters[Key] in verse:
clusters.add(Key, +1) # tries to increment the value of
# the key if the key is in the string verse.
else:
print "not in"
有人可以给我一些关于去哪里的建议吗?
谢谢