Python Forum
array change - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: array change (/thread-37517.html)



array change - svm - Jun-21-2022

Hi,

can you help me to understand where I'm wrong in coding?
a = [] # define first array (future matrix)
arr = [] # define a second array(matrix too)
while True:
    text = input()

    if text == 'end': # if typed end, then create matrix from inputed numbers
        break
    a.append([int(j) for j in text.split()]) # fullfill the matrix

for i in range(len(a)): 
    for k in range(len(a[i])):
        arr[i][k]= a[i][k+1] + a[i][k-1] + a[i+1][k] + a[i-1][k] # create new matrix as a sum of all nearby numbers
        k += 1
    i += 1
print(arr)



RE: array change - deanhystad - Jun-21-2022

arr = a does not do a copy, it sets both variables to refer to the same array objecr, the one referenced by a.. Changes to arr will be reflected in a because they really are changes to a, or more accurately the array referenced through a.

Other possible problems may be from strings being immutable in Python, meaning str objects cannot be modified.. You can use indexing to get a character in a strink, but you cannot use it to set a character.

Your indexing math is guaranteed to result in out of range errors.

Please surround posted code with python tags. There is a button in the editor for doing this.


RE: array change - svm - Jun-21-2022

Code edited as python code.


RE: array change - deanhystad - Jun-21-2022

Is this the same code? Was I hallucinating? Please do not change posted code after someone has written a reply to your post. Write a new post with the modified code.

You still have problems with array indexing. If you have a 3x3 array your code will try to access a[-1] and a[3]. a[-1] will probably be a big surprise and a[3] will raise an index error exception. The same goes for a[i][-1] and a[i][3].

I don't know how to fix this problem since I have no idea what this code is supposed to do. Can you treat the out of range index values as 0?
def get(m, i, k):
    if 0 <= i < len(m) and 0 <= k < len(m[i]):
        return m[i][k]
    return 0
If you are using i in a for loop you should not be incrementing i. The incrementing is done automatically by the range() command.

arr is an empty array. You cannot index an empty array. You demonstrated that you know about list comprehensions. I would use one of those to build you "a" matrix a row at a time. Use a list comprehension to make a row, and append the row.
arr = []
for i in range(len(a)):
   row = ([ row list comprehension goes here])
   arr.append(row)



RE: array change - svm - Jun-21-2022

I'm not sure that this function can help. Program don't know how many columns will be typed.
Here is sample input and output:
sum must me on positions (i-1, k), (i+1, k), (i, k-1), (i, k+1)
Sample Input 1:
9 5 3
0 7 -1
-5 2 9
end
Sample Output 1:
3 21 22
10 6 19
20 16 -1
Sample Input 2:
1
end
Sample Output 2:
4


RE: array change - deanhystad - Jun-21-2022

The function doesn't require any prior knowledge of the len of a or any of the row (lists) in a. It uses len() to get that information.

From the output it looks like out of range indices should "wrap-around". From your example a[-1] changes to a[2] and a[3] changes to a[0]. You should read up about lists and how it handles different index values. You'll need something that checks the range of the index values and does the wrap-around when needed.


RE: array change - svm - Jun-23-2022

a = []

while True:
    text = input()

    if text == 'end':
        break
    a.append([int(j) for j in text.split()])
arr = []

for i in range(len(a)):
    for k in range(len(a[i])):
        arr.append(a[i-1][k] + a[(i+1)%len(a)][k] + a[i][k-1] + a[i][(k+1)%len(a[i])])
        k += 1
    i += 1
print(arr)
here is output:
svm@ubuntu:~$ python3 spiski9.py
9 5 3
0 7 -1
-5 2 9
end
[3, 21, 22, 10, 6, 19, 20, 16, -1]

how to create output like this:
3 21 22
10 6 19
20 16 -1