问题描述
在我的代码中,我从文本文件的输入中读取值。我将它们转换为 numpy 数组,然后使用它们。 我迭代地加载这个输入。现在我正在尝试设置条件,如果一行中的最后一个值大于下一行中的第一个值,它将将该行中的每个值增加指定的值。在这种情况下,它会增加 10。
我的输出
[[11 12 13 14 15 16 17 18 19 20 21]
[21 22 23 24 25 26 27 28 29 30 31]]
[[31 32 33 34 35 36 37 38 39]
[41 42 43 44 45 46 47 48 49]
[51 52 53 54 55 56 57 58 59]]
[[61 62 63 64 65 66 67 68 69 70 71]
[71 72 73 74 75 76 77 78 79 80 81]
[81 82 83 84 85 86 87 88 89 90 91]]
我也部分成功了,但每次迭代都从头开始,我需要它从上一次迭代继续。
sedem[1:][sedem[:-1,-1] <= sedem[1:,0]] += 10
[[11 12 13 14 15 16 17 18 19 20 21]
[31 32 33 34 35 36 37 38 39 40 41]]
[[31 32 33 34 35 36 37 38 39]
[51 52 53 54 55 56 57 58 59]
[61 62 63 64 65 66 67 68 69]]
[[ 61 62 63 64 65 66 67 68 69 70 71]
[ 81 82 83 84 85 86 87 88 89 90 91]
[ 91 92 93 94 95 96 97 98 99 100 101]]
我需要:
[[11 12 13 14 15 16 17 18 19 20 21]
[31 32 33 34 35 36 37 38 39 40 41]]
[[51 52 53 54 55 56 57 58 59]
[61 62 63 64 65 66 67 68 69]
[71 72 73 74 75 76 77 78 79]]
[[ 61 62 63 64 65 66 67 68 69 70 71]
[ 81 82 83 84 85 86 87 88 89 90 91]
[ 91 92 93 94 95 96 97 98 99 100 101]]
我也尝试组合数组,然后像这样输入条件,然后再次拆分,但它不起作用,因为输入逐渐迭代它,所以这是不可能的。
基本上,我想确保数字不能重复
import os
import sys
import numpy as np
#read data
f = open("demofile.txt","r")
lines = f.readlines()
#input preprocessing
p=1
for i in list(lines):
if i[0] != '<' and i[0] != '>' and i[0] != '=':
d = str(' '.join(i.split()))
print(d)
else:
w = i.replace("=",'')
w = w.replace(">",'')
w = w.replace("<",'')
w = ','.join(w.split())
c=np.array([w])
c1 = [int(i) for i in c[0].replace(" ","").split(",")]
#insert input to array
c1=np.array(c1)
# save first value in array
frst=c1[0]
#remove first value in array
c1=np.delete(c1,0)
n=len(c1)
#multiply array
c1=np.array([c1]*frst)
#print(c1)
#transpose array
c1=np.transpose(c1)
#adding a value to each row
left = np.array([[(p+j) * 10 for j in range(frst)]] * n) +c1
#left = np.array([[(p+j) * 10 for j in range(frst)]] * n) +c1
left=left*-1
#print(left,'\n')
sedem=np.transpose(left)*-1
sedem[1:][sedem[:-1,-1] < sedem[1:,0]] *= 10
print(sedem,'\n')
p +=frst
演示文件.txt
<=2 1 2 3 4 5 6 7 8 9 10 11
<=3 1 2 3 4 5 6 7 8 9 10 9
<=3 1 2 3 4 5 6 7 8 9 10 11
可以以某种方式完成吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)