Tuesday 28 April 2009

python 真的很好用

今天做course project,一个expert system, 用clips实现,需要生成很多很多条rule的。。。

于是决定用Python写一个rule generator,真是好用了,用里面dict的功能,太强大了。

根据dict来进行排序啊,存取任意的一个元素啊,都十分方便。

贴一段代码片段。。写的比较烂。。。还是没有学会用Python的方式去写代码。。。。



def makeoutput2(new_dict):
newlistK = []

file_q = open("question.txt")
allLines = file_q.readlines()
file_q.close()
question_content = ""
file = open("output2.txt",'w')
number = 2
for eachkey in sorted(new_dict.items(), key = lambda d:d[1],reverse=True):
for eachLine in allLines:
if eachLine.find(eachkey[0]) !=-1:
num = eachLine.find('.')
question_content = eachLine[num+1:len(eachLine)-1]
break
file.write("\n(question (order "+str(number)+") (content \""+question_content+"\"))")
number = number + 1





No comments:

Post a Comment