Python Forum
Transactions between the list on python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Transactions between the list on python
#1
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
Reply
#2
>>> 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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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
Reply
#4
(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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
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
Reply
#6
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Generating accounting transactions data Emekadavid 0 387 Sep-22-2023, 09:00 AM
Last Post: Emekadavid
  Finding the price based on industry and number of transactions chandramouliarun 0 897 Jul-26-2022, 07:36 PM
Last Post: chandramouliarun

Forum Jump:

User Panel Messages

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