1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Python Append Function Malfunctioning

Discussão em 'Python' iniciado por Stack, Abril 7, 2025.

  1. Stack

    Stack Membro Participativo

    I am using trinket.io, an online IDE for python3. I currently do not have access to any other IDEs for download. I am trying to create a numpy array using numerous iterative and conditional statements. When I print each list, I receive the correct results that I am expecting but when I append it to the array, it results in very weird results.

    This is my code:

    import numpy as np
    import matplotlib.pyplot as plt
    from sympy import *

    x = Symbol('x')
    y = Function('f')(x)

    #Equation is ay_(n-1) + by_n + cy_(n+1) = d
    a = 3
    b = 4
    c = 6
    d = 4

    #Final row equation: ey_(n-2)+fy_(n-1)+gy_(n) = d
    e = 5
    f = 3
    g = 7
    #Timestep
    h = 0.2

    #Initialize variables
    y_0 = 0
    final_point = 1


    array = []
    row = []
    i = 1
    array_row_size = int(final_point / h)
    array_column_size = array_row_size + 1

    for i in range(1,array_row_size+1):
    row.clear()
    if i == 1:
    row.append(b)
    row.append(c)
    for j in range(2, array_column_size-1):
    row.append(0)
    row.append(d)
    elif i == 2:
    row.append(a)
    row.append(b)
    row.append(c)
    for j in range(3, array_column_size-1):
    row.append(0)
    row.append(d)
    elif i < (array_column_size - 1) and i>2:
    for j in range(2,i):
    row.append(0)
    row.append(a)
    row.append(b)
    row.append(c)
    for j in range(i+1, array_column_size-1):
    row.append(0)
    row.append(d)
    else:
    for j in range(1, array_column_size-3):
    row.append(0)
    row.append(e)
    row.append(f)
    row.append(g)
    row.append(d)
    array.append(row)
    print("Row:", row)
    print("Array", array)

    i += 1

    #print(array)


    and these are the results:

    Row: [4, 6, 0, 0, 0, 4]
    Array [[4, 6, 0, 0, 0, 4]]
    Row: [3, 4, 6, 0, 0, 4]
    Array [[3, 4, 6, 0, 0, 4], [3, 4, 6, 0, 0, 4]]
    Row: [0, 3, 4, 6, 0, 4]
    Array [[0, 3, 4, 6, 0, 4], [0, 3, 4, 6, 0, 4], [0, 3, 4, 6, 0, 4]]
    Row: [0, 0, 3, 4, 6, 4]
    Array [[0, 0, 3, 4, 6, 4], [0, 0, 3, 4, 6, 4], [0, 0, 3, 4, 6, 4], [0, 0, 3, 4, 6, 4]]
    Row: [0, 0, 5, 3, 7, 4]
    Array [[0, 0, 5, 3, 7, 4], [0, 0, 5, 3, 7, 4], [0, 0, 5, 3, 7, 4], [0, 0, 5, 3, 7, 4], [0, 0, 5, 3, 7, 4]]


    This is obviously very different that expected since I would expect that each row would be different in the array whereas in each iteration, all the rows are being updated and they are the same.

    Continue reading...

Compartilhe esta Página