我已經寫了一些代碼來根據here描述的標準從存儲cnf的文件加載cnf。將連接範式的文件加載到列表中
該文件是:
c simple_v3_c2.cnf // lines bigining by c are comments
c
p cnf 3 2 // the line bigining by p is the description of the pb
1 -3 0 // folowing lines are formulation of the pb, with 0 as ending caractere
2 3 -1 0
我想將其加載到[[1,-3] [2,3,-1]]
我寫作品的代碼,但它對我來說似乎很醜陋。我會對它有一些反饋感興趣。 (我是python的新手)。
def loadCnfFile(fileName='example.cnf'):
""" retourne une liste de listes d'entiers decrivants la forme normale conjonctive"""
cnf=[]
cnfFile = open(fileName, 'r')
for line in cnfFile:
if line[0]!="c" and line[0]!="p":
l=line.split("0")[0].strip().split(" ")
m=[]
for k in l:
m.append(int(k))
cnf.append(m)
cnfFile.close()
return cnf
謝謝!
我不確定我喜歡'line.split(「0」)'。如果他們得到像'1 2 10 3'這樣的東西呢? – georg
你說得對。我調整了我的樣本。現在使用rstrip(「0」),所以只有尾部零被刪除。 –
或只是'rstrip(「0 \ n」)'。將調整我的樣本。 –