Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Append to Array Fails
#1
The Script that I will post will show 2 different results Function - def Report_Results_Fail(): -- Fails Function - def Report_Results_Ok(): -- is OK

I am trying to understand why the first fails
def Processes_Last_Runs_Blanks_Clear(v_User_List):
    v_Blanks_Clear = [] ; v_Blanks_Clear.append("--||--")
    for i in range(len(v_User_List)):
        v_This = "-"#+" " * v_User_Len
        #v_This = v_This[0:v_User_Len]
        v_Blanks_Clear.append(v_This)
    return v_Blanks_Clear

def Active_User_List():
    # Read from file so array looks like this
    v_Users_Active = []
    v_Users_Active.append("Jeff")
    v_Users_Active.append("Kenny")
    v_Users_Active.append("Lenny")
    v_Users_Active.append("Manny")
    v_Users_Active.append("Nanny")
    return v_Users_Active

def Report_Results_Fail():
    v_Users_Active = Active_User_List()
    v_Blanks = Processes_Last_Runs_Blanks_Clear(v_Users_Active)
    v_Report = []
    v_Report.append(v_Blanks)
    v_Report[0][0] = "Process"
    v_Report[0][1] = "Users"
    for i in range(5):
        if i == len(v_Report):
            v_Report.append(v_Blanks)
        v_Report[i][1] = str(i)
    for i in range(len(v_Report)):
        print i,v_Report[i]

def Report_Results_Ok():
    v_Users_Active = Active_User_List()
    v_Report = []
    v_Report.append(Processes_Last_Runs_Blanks_Clear(v_Users_Active))
    v_Report[0][0] = "Process"
    v_Report[0][1] = "Users"
    for i in range(5):
        if i == len(v_Report):
            v_Report.append(Processes_Last_Runs_Blanks_Clear(v_Users_Active))
        v_Report[i][1] = str(i)
    for i in range(len(v_Report)):
        print i,v_Report[i]

print "Running on Imac High Sierra 10.13.6 - Python Version  2.7.10"
print "Report_Results_Fail()"
Report_Results_Fail()
print "Report_Results_Ok()"
Report_Results_Ok()
>>>> Result
Running on Imac High Sierra 10.13.6 - Python Version 2.7.10

Report_Results_Fail()
Output:
0 ['Process', '4', '-', '-', '-', '-'] 1 ['Process', '4', '-', '-', '-', '-'] 2 ['Process', '4', '-', '-', '-', '-'] 3 ['Process', '4', '-', '-', '-', '-'] 4 ['Process', '4', '-', '-', '-', '-']
Report_Results_Ok()
Output:
0 ['Process', '0', '-', '-', '-', '-'] 1 ['--||--', '1', '-', '-', '-', '-'] 2 ['--||--', '2', '-', '-', '-', '-'] 3 ['--||--', '3', '-', '-', '-', '-'] 4 ['--||--', '4', '-', '-', '-', '-']
Reply
#2
The reason they are giving different outputs if v_Blanks, which is a list you append to v_Report. On line 24, you change the copy you appended to v_Report, but lists don't copy unless you do it explicitly. Instead, you get a pointer to the old list. You when you change the version in v_Report, you change the original v_Blanks as well. On line 29, you change the latest v_Blanks that you appended to v_Report. But again, all the v_Blanks your are appending are just references to the same v_Blanks, so the output looks like you changed every copy the same way, because they're all the same list.

To explicitly copy a list, do a whole list slice: v_Blanks[:]. Note this is a "shallow" copy, any lists or other mutable objects in v_Blanks will be copied as references.

Also, you should switch to Python 3. At the end of the year support for 2.7 will stop.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Cant Append a word in a line to a list err "str obj has no attribute append Sutsro 2 2,567 Apr-22-2020, 01:01 PM
Last Post: deanhystad
  append list to empty array SchroedingersLion 1 2,166 Feb-02-2020, 05:29 PM
Last Post: SchroedingersLion
  Trying to append List/array based on condition Mr_Keystrokes 1 2,331 Mar-20-2018, 04:11 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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