简体   繁体   English

如何在每次迭代中更改数组中的值?

[英]How to change the value in the array in each iteration?

I have matrices of different sizes and I try to increase the value in each row with each iteration.我有不同大小的矩阵,我尝试在每次迭代中增加每一行的值。

I managed to increase the value in each row, but when the iteration moves to the next matrix, it only increases the value from the first row of the previous matrix I would need it to proceed from the last row of the matrix我设法增加了每一行的值,但是当迭代移动到下一个矩阵时,它只会增加前一个矩阵第一行的值我需要它从矩阵的最后一行开始

Is the way to save the last iteration from the previous matrix and continue the iteration in the next matrix?是从前一个矩阵中保存最后一次迭代并在下一个矩阵中继续迭代的方法吗?

here is the part of the code where I iterate the arrays这是我迭代 arrays 的代码部分

c1=c1 + 10 * (np.arange(c1.shape[1]) + 1)*p
p +=1

My result我的结果

#first iteration
11 12 13 
21 22 23   
#second iteration
21 22 23 
41 42 43 
61 62 63   
#third iteration    
31 32 33 
61 62 63  

I need我需要

11 12 13 
21 22 23   

31 32 33 
41 42 43 
51 52 53   

61 62 63 
71 72 63

This is my full code where reading input from text file这是我从文本文件中读取输入的完整代码

demofile.txt演示文件.txt

>=2 1 2 3 
>=3 1 2 3 
>=2 1 2 3

full code完整代码

import os
import sys
import numpy as np
import re

f = open("demofile.txt", "r")
lines = f.readlines()
p=1
#sys.stdout = open("results.txt", "w")
for i in list(lines):
     if i[0] != '<' and i[0] != '>' and i[0] != '=':# change txt file for np.array
        p = str(' '.join(i.split())) 
        print(p)
        
     else:
        w = i[3:]
        frst=i[2]
        w = ', '.join(w.split())
        y = i[2]
        y=int(y)+1
        #INSERT input to array
        c=np.array([w])
        c1 = [int(i) for i in c[0].replace(" ", "").split(",")]

        frst=str(frst).replace("[",'')
        frst=str(frst).replace("]",'')
        frst=int(frst)
        c1=np.array(c1)        

        sest2=c1
        c1=np.array([c1]*frst) #frst is the first value by which the matrix is multiplied
        c1=np.transpose(c1)#transpose
        

        #change value every iteration
        c1=c1 + 10 * (np.arange(c1.shape[1]) + 1)*p
        left=c1*-1
        sedem=np.transpose(left)*-1 #transpose matrix
        p +=1
       
        #editing outputs
        sedem=str(sedem).replace("[",'')
        sedem=str(sedem).replace("]",' 0')
        sedem=str(sedem).replace(".",'')
        sedem = sedem[:-1]
        sedem=str(sedem).replace("\n ",'\n')
        sedem=str(sedem).replace("  ",' ')
        sedem=str(sedem).replace("\n ",'\n')
        sedem=str(sedem).lstrip()       
        print(sedem,'\n')

Try this:尝试这个:

        #change value every iteration
        c1 = np.array([[(p + j) * 10 for j in range(frst)]] * 3) + c1
        left=c1*-1
        sedem=np.transpose(left)*-1 #transpose matrix
        p += frst

You track p stepping by frst and make a matrix of elements which are 10's multiples based on p .您跟踪p frst并根据p创建一个元素矩阵,该矩阵是 10 的倍数。

For example, if p == 3 , the matrix will be例如,如果p == 3 ,矩阵将是

[[30 40 50]
 [30 40 50]
 [30 40 50]]

Lets try something link this with more usage of numpy to solve the problem.让我们尝试将其与 numpy 的更多使用联系起来以解决问题。 Only use iterator to break the above array into chunks for each input.仅使用迭代器将上述数组分解为每个输入的块。

You can easily modify your code to get the input from the txt file to a numpy array such that each line in text is a single row in an array.您可以轻松地修改代码以将输入从 txt 文件获取到 numpy 数组,以便文本中的每一行都是数组中的单行。

#Setup each row in text file as a row in a np matrix

arr = np.array([[2,1,2,3],
                [3,1,2,3],
                [2,1,2,3]])

print('INPUT:')
print(arr)
print('')

ones = np.repeat(arr[:,1:], arr[:,0], axis=0)
tens = (np.arange(1,arr[:,0].sum()+1)*10)
nums = ones+tens[:, None]

print('nums:')
print(nums)
print('')

#Splitting nums for each input
l = np.cumsum([0]+list(arr[:,0]))
chunks = [nums[i:j,:] for i,j in zip(l,l[1:])]

print('OUTPUT:')
for i,chunk in enumerate(chunks):
    print('chunk number',i,':')
    print(chunk)
    print('')
INPUT:
[[2 1 2 3]
 [3 1 2 3]
 [2 1 2 3]]

nums:
[[11 12 13]
 [21 22 23]
 [31 32 33]
 [41 42 43]
 [51 52 53]
 [61 62 63]
 [71 72 73]]

OUTPUT:
chunk number 0 :
[[11 12 13]
 [21 22 23]]

chunk number 1 :
[[31 32 33]
 [41 42 43]
 [51 52 53]]

chunk number 2 :
[[61 62 63]
 [71 72 73]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM