Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个清单:
KB = [['~p', '~r', 's'], ['~r', 'k'], ['~k', 'm'], ['r'], ['~m'], ['~p', 'r']]
我需要输出到 CNF 格式的文件('output.txt'),如下所示:
~p|~r|s ~r|k ~k|m r ~m ~p|r
那我现在该怎么办?
BearBrown 的评论涵盖了问题中最重要的部分。我正在适应它,显示如何根据需要在加入管道('|')后写入文件。
KB = [['~p', '~r', 's'], ['~r', 'k'], ['~k', 'm'], ['r'], ['~m'], ['~p', 'r']] p = ["|".join(x) for x in KB] with open('output.txt') as o: for item in p: o.write('%s\n' % item)