[英]Convert a list of tuples (from itertools) into a list of words in a text file, Python
I'm using:我在用着:
list(it.permutations(wordlist, 3))
and am getting a result as a list of tuples, such as:并得到一个元组列表的结果,例如:
[('mom', 'dad', 'kid'), ('mom', 'dad', 'house'), ('mom', 'dad', 'car'), ('mom', 'dad', 'happy'), ('mom', 'dad', 'Hello'), ('mom', 'dad', 'goodbye'), ('mom', 'dad', 'covid'), ('mom', 'dad', 'influenza'), ('mom', 'dad', 'cold'), ('mom', 'dad', 'car'), ('mom', 'dad', 'escape')...]
but what I really want is a.txt file like:但我真正想要的是一个 .txt 文件,例如:
mom dad kid\n
mom dad house\n
mom dad car\n
... etc
How do I either iterate permutations straight into human readable rows, or write/convert this existing list into a file?我如何将排列直接迭代到人类可读的行中,或者将这个现有列表写入/转换成一个文件? THANKS (first question ever!)
谢谢(第一个问题!)
with open('path/to/output', 'w') as outfile:
for sent in it.permutations(wordlist, 3):
outfile.write(f"{' '.join(sent)}\n")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.