Python Forum
Help With While Loop and Append - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Help With While Loop and Append (/thread-15758.html)

Pages: 1 2


Help With While Loop and Append - laprus - Jan-30-2019

Hi everyone. I am using python for coding an algorithm and I'm struggling with usage of while and append.

the part of code is:
print('isis')
print(temp11)
print(main_list[9][6])  # this variable's value is different now
temp_cluster.clear()
print(temp_cluster)
temp_cluster.append(Node_Names[temp11])
print(main_list[9][6])   # and the same variable's value is different now
Later on, I am changing main_list[9][6] to temp_cluster. but that is after the above sentences.
However the value of variable changes midway.

Here is same output:
Output:
isis 10 ['j', 'i'] # as you can see value has changed from 'j' 'i' to 'k' after 2 sentences [] ['k'] exy
Can anyone help me with this?


RE: Help With While Loop and Append - buran - Jan-30-2019

please, provide minimal, complete and verifiable example that reproduce your problem. we cannot guess what the problem is.


RE: Help With While Loop and Append - laprus - Jan-31-2019

Thanks. I'll be careful henceforth.

I'll need a day to write a similar/smaller problem with same error. Will surely do it as this problem is coming again in another code.


RE: Help With While Loop and Append - perfringo - Jan-31-2019

(Jan-30-2019, 11:02 AM)laprus Wrote: I'm struggling with usage of while and append.

In code snippet provided I can't observe any while. But I do observe temp11, main_list, temp_cluster with no idea what they are.


RE: Help With While Loop and Append - laprus - Feb-03-2019

Hi everyone, here's a small code that I've written that has same error.


Node_Names = ['a', 'b', 'c', 'd']
Values_of_label = [1, 2, 3, 4]
main_list = list()
Even_Or_Odd = ['NA' ,'NA', 'NA', 'NA']
for i in range(len(Node_Names)):
    main_list.append([Node_Names[i], Values_of_label[i], Even_Or_Odd[i]])
s = list()
temp0 = 0
print(main_list)
while (temp0<len(Node_Names)):
    if ((main_list[temp0][1] == 1) or (main_list[temp0][1] == 3)):
        s = ['number is even', 'number is not odd']
    else:
        s = ['number is odd', 'number is not even']
    main_list[temp0][2] = s
    temp0 = temp0 + 1
    s.clear()

print(main_list)
I get this output:

Output:
[['a', 1, 'NA'], ['b', 2, 'NA'], ['c', 3, 'NA'], ['d', 4, 'NA']] [['a', 1, []], ['b', 2, []], ['c', 3, []], ['d', 4, []]] Process finished with exit code 0
Why is line 15 of code not working? It works when I use tuple though. As in main-list[temp0][2] = tuple(s)


RE: Help With While Loop and Append - perfringo - Feb-03-2019

(Feb-03-2019, 09:19 AM)laprus Wrote: Hi everyone, here's a small code that I've written that has same error.
...
Why is line 15 of code not working? It works when I use tuple though. As in main-list[temp0][2] = tuple(s)

I didn't observe any error. Error is something that Python raises when interpreter don't know how to interpret command given (NameError, IndexError, ValueError etc).

Regarding you code - it seems to me complex solution to something. What do you want to accomplish?

Container objects (list of lists in this case) keep references to the object. You add reference to list s into main_list and then you clear s. Of course it will clear the value in main_list as well as they point to same object. Lists are mutable objects and tuples are not.

To see how it works you can modify last rows:

main_list[temp0][2] = s
print(f's added {main_list}')
temp0 = temp0 + 1
s.clear()
print(f's cleared {main_list} ')
Output:
s added [['a', 1, ['number is even', 'number is not odd']], ['b', 2, 'NA'], ['c', 3, 'NA'], ['d', 4, 'NA']] s cleared [['a', 1, []], ['b', 2, 'NA'], ['c', 3, 'NA'], ['d', 4, 'NA']] s added [['a', 1, []], ['b', 2, ['number is odd', 'number is not even']], ['c', 3, 'NA'], ['d', 4, 'NA']] s cleared [['a', 1, []], ['b', 2, []], ['c', 3, 'NA'], ['d', 4, 'NA']] s added [['a', 1, []], ['b', 2, []], ['c', 3, ['number is even', 'number is not odd']], ['d', 4, 'NA']] s cleared [['a', 1, []], ['b', 2, []], ['c', 3, []], ['d', 4, 'NA']] s added [['a', 1, []], ['b', 2, []], ['c', 3, []], ['d', 4, ['number is odd', 'number is not even']]] s cleared [['a', 1, []], ['b', 2, []], ['c', 3, []], ['d', 4, []]]
You can observe that you replace 'NA' with value of s and then you clear that value.


RE: Help With While Loop and Append - buran - Feb-03-2019

when you add s to each element of main list you actually add reference to same list object s. Then on line 17 you clear() list s. But it is referenced also from lists in the main_list, so you effectively clear all of them.
spam = [1, 2, 3]
eggs = []
eggs.append(spam)
print(spam)
print(eggs)
spam.clear()
print(spam)
print(eggs)
Output:
[1, 2, 3] [[1, 2, 3]] [] [[]]
check this link for more detailed explanaition
https://nedbatchelder.com/text/names.html


RE: Help With While Loop and Append - laprus - Feb-05-2019

Wow! I didn't know that when I do 'any variable' = 'some other variable' the variable gets linked forever. I thought only the values are transferred. If i have to transfer only the value of the variable and not link them forever, any idea how to do it?

This is another example of what I want to achieve.
Basically I have a list of variables say, [a, b, c, d]
I have like 10 such lists (the number of lists are inputted from the user).
So i have: list1 [ a, b, c, d]
list2 has different values of [a, b, c, d]
list3 has different values of [a, b, c, d]
and so on...

and what i want to do is: based on particular values of 'b' and 'c' some of the values of 'a' will get copied to 'd' and not all.
like for example: if in 'list1' here if 'a' is [2, 3, 4, 5, 6, 7]
and if 'b' and 'c' are both '1' then 'd' will take only 'odd' numbers from 'a' and 'd' = [3, 5, 7]
but if 'b' and 'c' are both '0' then 'd' will take only 'even' numbers from 'a' and 'd' = [2, 4, 6]
and if 'b' = 1 and 'c' = 0, then 'd' will take first three numbers of 'a' and 'd' = [2, 3, 4]
but if 'b' = 0 and 'c' = 1, then 'd' will take last three numbers of 'a' and 'd' = [5, 6, 7]

and this should happen for all the lists.
so what i'm doing is:
number_of_lists = int(input("enter number of lists: "))
temp =0
while (temp<(number of lists)):
....(above code or something like that)

i'm using 'if' block for 'b' and 'c' and then based on condition i'm transferring some values of 'a' to 's'
and then i'm transferring those values of 's' to 'd' at the end.

i've used integers in this post but in the homework question i've to use letters.

* sorry for the late reply. university can get hectic at times.


RE: Help With While Loop and Append - perfringo - Feb-05-2019

(Feb-05-2019, 05:09 AM)laprus Wrote: Wow! I didn't know that when I do 'any variable' = 'some other variable' the variable gets linked forever. I thought only the values are transferred. If i have to transfer only the value of the variable and not link them forever, any idea how to do it?


Your conclusions are wrong on so many levels that I don't make any attempt to correct you myself. I suggest you study Ned Batchelder Python Names and Values (watch both the video and slides, repeatedly if needed)


RE: Help With While Loop and Append - buran - Feb-05-2019

(Feb-05-2019, 05:09 AM)laprus Wrote: I didn't know that when I do 'any variable' = 'some other variable' the variable gets linked forever.
I didn't say that. I will reiterate my advice as well @perfingo's advice to study https://nedbatchelder.com/text/names.html