Python 和列表组合
Python and list combination
我是 python 的新手,通常在编程方面很沮丧。
我有任务要完成。我需要加载 file.txt(每行有一个字母),然后用所有组合创建另一个 file2.txt。这是我设法启动的代码
from itertools import product
def main():
list = ('A', 'B', 'C', 'D')
for word in product(list, repeat=2):
print (''.join(word))
if __name__ == "__main__": main()
它给了我这个
AA
AB
AC
AD
BA
BB
BC
BD
CA
CB
CC
CD
DA
DB
DC
DD
这个不行。
from itertools import product
def main():
list = open('recnik2.txt', 'a') #I gess python cant read like this
for word in product(list, repeat=2):
print (''.join(word))
if __name__ == "__main__": main()
它给了我这个错误
Traceback (most recent call last):
File "C:\Users\"Ideletedthis"\product.py", line 11, in <module>
if __name__ == "__main__": main()
File "C:\Users\"Ideletedthis"\product.py", line 7, in main
for word in product(list, repeat=2):
io.UnsupportedOperation: not readable
Any1 可以解释原因,也许可以提供一些更好的解决方案
操作系统在处理文件时为您提供多种选择。一种选择是阅读内容。另一种是写信给它。您通常必须指定要执行的每个选项才能执行此操作。这是一种安全机制。
在您的代码中,您要求 OS,通过 Python,到 "append",再到 recnik2.txt
文件。追加是一种将内容写入 到文件的方法,从文件末尾开始。但是由于您正在尝试读取仅使用 "write" 访问权限打开的文件,因此 OS 告诉您文件 "isn't readable."
尝试用 "read" 打开打开 open('recnik2.txt', 'r')
访问。 Read this 如果您对其他选项感到好奇。
我设法用 "read" 模式纠正了这个问题,并用 "write" 创建了另一个文件。
但是我学得越多,我的问题就越多。这是代码
from itertools import product
def main():
infile = open('recnik2.txt', 'r')
outfile = open('recnik3.txt', 'w')
for word in product(infile, repeat=2):
print (''.join(word), file=outfile)
infile.close()
outfile.close()
if __name__ == "__main__": main()
它给了这个。
A
A
A
B
A
C
A
D
B
A
B
B
B
C
B
D
C
A
C
B
C
C
C
D
DA
DB
DC
DD
我知道 print() 必须打印可以用 end= '' 删除的空行。
但那是组合之间的界线。当它跳转到另一个字母时,那条线不存在。看看
A
D
B
A
B 和 D 确实有 space,所以如果我输入 end= '' 它将放置
A
DB
A
这令人困惑。为什么它会分开组合?为什么 AA 不相邻而 DD 正是我需要的。
对不起,如果我这是菜鸟的问题,但找不到解释这个的教程。
Python 指南有很多但不是很详细
我是 python 的新手,通常在编程方面很沮丧。 我有任务要完成。我需要加载 file.txt(每行有一个字母),然后用所有组合创建另一个 file2.txt。这是我设法启动的代码
from itertools import product
def main():
list = ('A', 'B', 'C', 'D')
for word in product(list, repeat=2):
print (''.join(word))
if __name__ == "__main__": main()
它给了我这个
AA
AB
AC
AD
BA
BB
BC
BD
CA
CB
CC
CD
DA
DB
DC
DD
这个不行。
from itertools import product
def main():
list = open('recnik2.txt', 'a') #I gess python cant read like this
for word in product(list, repeat=2):
print (''.join(word))
if __name__ == "__main__": main()
它给了我这个错误
Traceback (most recent call last):
File "C:\Users\"Ideletedthis"\product.py", line 11, in <module>
if __name__ == "__main__": main()
File "C:\Users\"Ideletedthis"\product.py", line 7, in main
for word in product(list, repeat=2):
io.UnsupportedOperation: not readable
Any1 可以解释原因,也许可以提供一些更好的解决方案
操作系统在处理文件时为您提供多种选择。一种选择是阅读内容。另一种是写信给它。您通常必须指定要执行的每个选项才能执行此操作。这是一种安全机制。
在您的代码中,您要求 OS,通过 Python,到 "append",再到 recnik2.txt
文件。追加是一种将内容写入 到文件的方法,从文件末尾开始。但是由于您正在尝试读取仅使用 "write" 访问权限打开的文件,因此 OS 告诉您文件 "isn't readable."
尝试用 "read" 打开打开 open('recnik2.txt', 'r')
访问。 Read this 如果您对其他选项感到好奇。
我设法用 "read" 模式纠正了这个问题,并用 "write" 创建了另一个文件。 但是我学得越多,我的问题就越多。这是代码
from itertools import product
def main():
infile = open('recnik2.txt', 'r')
outfile = open('recnik3.txt', 'w')
for word in product(infile, repeat=2):
print (''.join(word), file=outfile)
infile.close()
outfile.close()
if __name__ == "__main__": main()
它给了这个。
A
A
A
B
A
C
A
D
B
A
B
B
B
C
B
D
C
A
C
B
C
C
C
D
DA
DB
DC
DD
我知道 print() 必须打印可以用 end= '' 删除的空行。 但那是组合之间的界线。当它跳转到另一个字母时,那条线不存在。看看
A
D
B
A
B 和 D 确实有 space,所以如果我输入 end= '' 它将放置
A
DB
A
这令人困惑。为什么它会分开组合?为什么 AA 不相邻而 DD 正是我需要的。 对不起,如果我这是菜鸟的问题,但找不到解释这个的教程。 Python 指南有很多但不是很详细