Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
array change
#1
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)
Reply
#2
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.
Reply
#3
Code edited as python code.
Reply
#4
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)
Reply
#5
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
Reply
#6
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.
Reply
#7
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Change a numpy array to a dataframe Led_Zeppelin 3 1,115 Jan-26-2023, 09:01 PM
Last Post: deanhystad
Question Change elements of array based on position of input data Cola_Reb 6 2,134 May-13-2022, 12:57 PM
Last Post: Cola_Reb
  LIST or ARRAY Comparison and change of value nio74maz 0 1,703 Dec-21-2020, 05:52 PM
Last Post: nio74maz
  change array elements dependent on index SchroedingersLion 1 2,215 Nov-22-2019, 06:25 AM
Last Post: scidam
  change array column values without loop khalidreemy 2 3,790 May-05-2019, 09:05 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020