我有一个创建一个魔术正方形的程序,另一个应该读取此输出并验证它是一个魔术方形的程序。
使用
的第一个输出中读取的secound程序array = []
for zeile in sys.stdin:
zeile = str.strip(zeile)
array.append(zeile)
这将导致#(1(
['[17 24 1 8 15]', '[23 5 7 14 16]', '[ 4 6 13 20 22]', '[10 12 19 21 3]', '[11 18 25 2 9]']
我需要的是#(2(
[17 24 1 8 15]
[23 5 7 14 16]
[ 4 6 13 20 22]
[10 12 19 21 3]
[11 18 25 2 9]
是第一个程序制作的。
所以我的问题是如何格式化#(1(的结果,以便得到我需要的数组?(#(2((?
我找到了一个解决方案
我打印了我将第一个程序形成外部文件的标准输入,然后我读取并操纵它,以便满足我的需求
这是一些代码:
data = open('magischesDreieckErzeugt.txt', 'w')
for zeile in sys.stdin:
zeile = str.strip(zeile)
print(zeile, file=data)
if zeile == 'Bitte korrigieren Sie Ihre Eingabe!':
print('Korrigieren Sie Ihre Eingabe im 1. Teil des Programms!')
quit()
else:
print(zeile)
data.close()
并与:
一起阅读
with open('magischesDreieckErzeugt.txt') as infile:
quadrat = np.fromstring( infile.read().replace('[','').replace(']', ''), sep=' ')
# bestimme da maximum des arrays und ziehe die wurzel um die länge (= höhe) des arrays zu ermitteln
x = int(np.sqrt(np.max(quadrat)))
# formatiere das array in ein x mal x array im
quadrat = np.reshape(quadrat, (-1,x))