如何链接多个数组的条件?



我为一个数组创建了一个条件,如果一行中的最后一个值小于下一行中的第一个值,则下一行增加+10。我尝试为一行中的多个数组这样做,它从一行中的输入读取。问题是,对于每个矩阵,它都是从最开始工作的。有没有可能以某种方式串联起来,使得连续矩阵的条件相互遵循?

输入

[[11 12 13 14 15 16 17 18 19]
[21 22 23 24 25 26 27 28 29]] 
[[31 32 33 34 35 36 37 38 39 40 42]
[41 42 43 44 45 46 47 48 49 50 52]
[51 52 53 54 55 56 57 58 59 60 62]] 
[[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]
[21 22 23 24 25 26 27 28 29]] 
[[31 32 33 34 35 36 37 38 39 40 42]
[51 52 53 54 55 56 57 58 59 60 62]
[61 62 63 64 65 66 67 68 69 70 72] 
[[ 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]
[21 22 23 24 25 26 27 28 29]] 
[[31 32 33 34 35 36 37 38 39 40 42]
[51 52 53 54 55 56 57 58 59 60 62]
[61 62 63 64 65 66 67 68 69 70 72]] 
[[ 71  72  73  74  75  76  77  78  79  80  81]
[ 91  92  93  94  95  96  97  98  99  100  101]
[ 111  112  113  114  115  116  117  118  119 120 121]]

我在整个代码中读取demofile.txt中的值,并将它们转换为数组,然后操作它们

我还用注释附加了整个代码

完整代码:

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=left*-1
sedem=np.transpose(left)*-1


#condition
sedem[1:][sedem[:-1,-1] >= sedem[1:, 0]] += 10
print(sedem,'n')

p +=frst

demofile.txt

<=2 1 2 3 4 5 6 7 8 9  
<=3 1 2 3 4 5 6 7 8 9 10 12 
<=3 1 2 3 4 5 6 7 8 9 10 11

你只需要合并列表,使所有的行在一起

l1 = [[11,12, 13, 14, 15, 16, 17, 18, 19],
[21, 22, 23, 24, 25, 26, 27, 28, 29]] 
l2 = [[31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 42],
[41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 52],
[51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 62]] 
l3 = [[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]] 
biglist = l1
biglist.extend(l2)
biglist.extend(l3)
for index, item in enumerate(biglist):
try:
# if last element of l is greater than first element of next row
if item[len(item) - 1] > biglist[index + 1][0]:
print('adding 10 to row {}'.format(i))
biglist[index + 1] = [m + 10 for m in biglist[index + 1]]

except IndexError:
pass
biglist

输出一个大列表

中的行
adding 10 to row 7
adding 10 to row 7
adding 10 to row 7
adding 10 to row 7
adding 10 to row 7
[[11, 12, 13, 14, 15, 16, 17, 18, 19],
[21, 22, 23, 24, 25, 26, 27, 28, 29],
[31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 42],
[51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 62],
[61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 72],
[71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81],
[81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91],
[91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101]]

如果你需要保持分隔,你可以存储原始列表的长度(l1, l2和l3),然后根据这些长度拆分列表。

相关内容

  • 没有找到相关文章

最新更新