【问题标题】:How to chain a condition for multiple arrays?如何链接多个数组的条件?
【发布时间】:2021-05-22 18:07:51
【问题描述】:

我为数组创建了一个条件,如果一行中的最后一个值小于下一行中的第一个值,则将下一行增加 +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

可以将它连接起来,以便每个 nut 的条件不是从头开始而是继续吗?

我的输出:

[[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 中读取值并将它们转换为数组,然后对其进行操作

我还用 cmets 附上了整个代码

完整代码:

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

【问题讨论】:

    标签: python arrays numpy for-loop


    【解决方案1】:

    您只需要合并列表,以便所有行都在一起

    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)的长度,然后根据这些长度拆分此列表。

    【讨论】:

    • 它不会像这样工作,因为在我的代码中,字段会迭代地通过,我们无法连接它们,下一个不能是其他方式吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-15
    • 2021-07-29
    • 1970-01-01
    • 2022-10-06
    • 2020-08-14
    相关资源
    最近更新 更多