Python Forum
Transactions between the list on python - 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: Transactions between the list on python (/thread-12130.html)



Transactions between the list on python - juniorcoder - Aug-10-2018

Hello everyone ,

I need a small help on python.
I have a list like a=[1 2 3 4 5 ]
I need to obtain abother list by summing second and third index and making third index equal to 0 which is
a=[1 5 0 4 5]
So far there is no problem.
The problem when it is needed in the following parts of the code, i must go back to the original list which is a=[1 2 3 4 5];
but since I did a[2]=0 (to make third index equal to zero), I can not go back to original list. I guess I should save the original list somehow but even when I save , when I said make a[2]=0 , saving list also changes .
Can you help me, how can I achieve this task , please ? Wall how can I turn original list , or call original list when I need ?
Thank you


RE: Transactions between the list on python - buran - Aug-10-2018

>>> a = [1, 2, 3, 4, 5]
>>> b = a[::]
>>> a[1:3] = [a[1]+a[2],0]
>>> a
[1, 5, 0, 4, 5]
>>> b
[1, 2, 3, 4, 5]
>>> 
lists are mutable, that is why you need to make a copy of list a, i.e. b = a[::] and not b = a


RE: Transactions between the list on python - juniorcoder - Aug-10-2018

Thank you so much Buran ,
I guess it will solve my problem. How can I do the same job for arrays instead of lists? Imagine this time , we will make one row of array is 0 and we want to go back to the original list time to time again ? How can I copy the n*n dimention arry and how can I do the same job for array ?
Millions thanks


RE: Transactions between the list on python - buran - Aug-10-2018

(Aug-10-2018, 05:17 PM)juniorcoder Wrote: How can I do the same job for arrays instead of lists?
list in python are what in other languages is called array, so I'm not sure what you mean by arrays instead of lists...
list can hold any type of objects, incl. other lists, i.e. list of lists


RE: Transactions between the list on python - juniorcoder - Aug-10-2018

For example

the first array is,
a=[1 2 3 4 5]
[0 0 0 2 3]
[0 1 0 5 6 ]]
I will sum first row and second row and I will make second row 0 ,lıke this
a=[1 2 3 6 8]
[0 0 0 0 0]
[0 1 0 5 6 ]]
When I need orıginal array (the versiın before modifying) I should be able to use the original one , the same thing what you did but this time n*n matrix , how can I do the same task in this case

I really stuck here , thank you so much for your eny help


RE: Transactions between the list on python - buran - Aug-10-2018

from copy import deepcopy
a=[[1, 2, 3, 4, 5],
   [0, 0, 0, 2, 3],
   [0, 1, 0, 5, 6]]

b = deepcopy(a)
a[1][1] = 10
print('a ==> {}'.format(a))
print('b ==> {}'.format(b))
a[0] = [i+j for i, j in zip(*a[:2])]
a[1] = [0, 0, 0, 0, 0]
print('a ==> {}'.format(a))
print('b ==> {}'.format(b))
a[1][1] = 10
print('a ==> {}'.format(a))
print('b ==> {}'.format(b))
Output:
a ==> [[1, 2, 3, 4, 5], [0, 10, 0, 2, 3], [0, 1, 0, 5, 6]] b ==> [[1, 2, 3, 4, 5], [0, 0, 0, 2, 3], [0, 1, 0, 5, 6]] a ==> [[1, 12, 3, 6, 8], [0, 0, 0, 0, 0], [0, 1, 0, 5, 6]] b ==> [[1, 2, 3, 4, 5], [0, 0, 0, 2, 3], [0, 1, 0, 5, 6]] a ==> [[1, 12, 3, 6, 8], [0, 10, 0, 0, 0], [0, 1, 0, 5, 6]] b ==> [[1, 2, 3, 4, 5], [0, 0, 0, 2, 3], [0, 1, 0, 5, 6]]
instead of b = a[::] you should use copy.deepcopy() function from copy module. This is relevant for compound objects (objects that contain other objects, like lists or class instances). There is also copy.copy() function.
For the ifference between the two read the documentation for copy module. There difference between shallow copy (i.e. copy.copy() or [::]) and deepcopy (copy.deepcopy() function is explained)

see the difference
from copy import copy
a=[[1, 2, 3, 4, 5],
   [0, 0, 0, 2, 3],
   [0, 1, 0, 5, 6]]

b = copy(a) # this is the same as b = a[::]
a[1][1] = 10
print('a ==> {}'.format(a))
print('b ==> {}'.format(b))
a[0] = [i+j for i, j in zip(*a[:2])]
a[1] = [0, 0, 0, 0, 0]
print('a ==> {}'.format(a))
print('b ==> {}'.format(b))
a[1][1] = 10
print('a ==> {}'.format(a))
print('b ==> {}'.format(b))
Output:
a ==> [[1, 2, 3, 4, 5], [0, 10, 0, 2, 3], [0, 1, 0, 5, 6]] b ==> [[1, 2, 3, 4, 5], [0, 10, 0, 2, 3], [0, 1, 0, 5, 6]] a ==> [[1, 12, 3, 6, 8], [0, 0, 0, 0, 0], [0, 1, 0, 5, 6]] b ==> [[1, 2, 3, 4, 5], [0, 10, 0, 2, 3], [0, 1, 0, 5, 6]] a ==> [[1, 12, 3, 6, 8], [0, 10, 0, 0, 0], [0, 1, 0, 5, 6]] b ==> [[1, 2, 3, 4, 5], [0, 10, 0, 2, 3], [0, 1, 0, 5, 6]]
the difference is in the second element of b list when it is printed for the first time