我已经编写了一些代码来从一个文件中加载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
谢谢!
我想对你的代码最好的反馈是用更"python化"的方式重写它。例如:
def cnf_lines(path):
"""Yields cnf lines as lists from the file."""
with open(path) as fp:
for line in fp:
if not line.startswith(('c', 'p')):
items = map(int, line.split())
yield items[:-1]
重点:
- PEP-8一致性(python中请不要使用camelCase)
- 文件操作的上下文管理器(
with
) - 生成器(
yield
)代替累积列表
注意:这段代码是有意简化的,并不完全支持你链接到的规范。
using list comprehension
:
In [66]: with open("example.cnf") as f:
print [map(int,line.split("0")[0].split()) for line in f if line and
not (line.startswith("c") or line.startswith("p"))]
....:
[[1, -3], [2, 3, -1]]
或:
with open("example.cnf") as f:
x= lambda y,c:y.startswith(c)
print [map(int,line.split("0")[0].split()) for line in f if line and
not any(x(line,z) for z in ("c","p"))]
....:
[[1, -3], [2, 3, -1]]
Ashwini的代码是正确的,并且对经验丰富的程序员很有吸引力(谢谢),但对于python新手(你似乎是)来说,可能一个普通的for循环更容易理解:
result = []
with open("example.cnf") as f:
for line in f:
if not (line.startswith("c") or line.startswith("p")):
result.append([int(x) for x in line.rstrip("0").rstrip("0n").split()])