List mutation and shared structure, Python Programming

Assignment Help:

List mutation and shared structure

Lists are mutable data  structures, which defines  that  we can actually modifies  the values  stored in their components. We do this by using element-selection statement, like a[1] on the left-hand side of an assignment expression. So, the assignment

a[1] = -3

assigns  the  second  element of a to be -3.  In more  detail,  the  left-hand side  of this  expression

1630_List mutation and shared structure.png

We have permanently modified the list named a.

We will defines the consequences of the mutability of lists; programs that change list structure can become  very  confusing, but  you  can always work  your  way  through what  is happening by drawing out the memory diagrams.Continuing the previous samples, let us remind that  a is bound directly to a pointer to a list (or a sequence of memory cells), and think  about  what  occurs if we do:

886_List mutation and shared structure 2.png

Now,  we can reference parts  of the list through b, and even modifies  the list structure that way:

 

>>> b[0]

2

>>> b[2] = 1

Notice that, because  a and b point  to the similar list, modifying b changes a!

989_List mutation and shared structure 3.png

This situation is called  aliasing : the name  b has become  an alias  for a.  Aliasing may be useful, but  it may also cause  problems, because  you  can  inadvertently modify  b (by passing it into a procedure that changes one of its structured statement, for example) when  it is very important to you to keep a unmodi?ed.

 

Another important way to change  a list is to add  or change  components. We will show adding elements to the end of a data structure, but look the Python documentation for more functions on lists. This statement

836_List mutation and shared structure 4.png

 

memory sequence), b is modified too. This is a side effect of the aliasing between a and b:

>>> b

[2, -3, 1, 9]

Often, it will be important to make a fresh copy of a list so that you can change it without affecting the  original one.   Here  are  two  similar types  to make  a copy  (use  whichever one  you  can remember):

>>> c = list(a)

>>> c = a[:]

Here is a sample of the memory at this position:

997_List mutation and shared structure 5.png

 

Now,  if we change  a component of c, it does not affect a (or b!):

 

>>> c[0] = 100

>>> c

[100, -3, 1, 9]

>>> a

[2, -3, 1, 9]

We can create crazy lists that share file within a single list:

>>> f = [1, 2, 3]

>>> g = [1, f, [f]]

472_List mutation and shared structure 6.png

 

If you want  to add  an element to a list and get a new copy at the similar time, you may do

>>> a + [1]

The + operator makes  a new list that contains the elements of both of its arguments, but does not give  any  top-level files.  All of our  functions of copying only work  reliably  if your  lists do not have other lists, because  it only copies single level of list. So example, if we did:

331_List mutation and shared structure 7.png

It is clear that if we were to modify  f, it would alter  h, so this is not a completely fresh copy.  If you have  to copy deep  structures, that  is, to create  a copy not only of the top level list structure, but  of the lists of any structures that  list has, and  the lists those  lists has,  etc., you  will have to use the Python copy.deepcopy method.

 


Related Discussions:- List mutation and shared structure

Python programming, Suppose the cover price of a book is $24.95, but bookst...

Suppose the cover price of a book is $24.95, but bookstores get a 40% discount. Shipping costs $3 for the first copy and 75 cents for each additional copy. What is the total whol

#Connect4, In English, specify a representation of the board game in Python...

In English, specify a representation of the board game in Python. The representation should capture the entire state of the game at one point in time. It may be helpful to first fi

Structured data, Structured data We will often  want  to work  with  l...

Structured data We will often  want  to work  with  large  groups of data.   Rather  than  providing  each  number its own  value,  we need  to manage the data  into natural s

Example of Procedures as First-class objects, Procedures in Firrst-class ob...

Procedures in Firrst-class objects In Python, unlike  many  other  languages, methods are behave in much  the same way as num­ bers:  they  can be stored as values  of variabl

File handling , A program to count how many files are on the file system wh...

A program to count how many files are on the file system which displays summary information regarding the total number of bytes used by all files and a breakdown of the number of b

Python programing, Task (Anagrams) Write a function that checks whether two...

Task (Anagrams) Write a function that checks whether two words are anagrams. Two words are anagrams if they contain the same letters. Fore example, silent and listen are anagrams.

Perimeter of a polygon, Perimeter of a polygon Now, let's consider the...

Perimeter of a polygon Now, let's consider the problem of computing the length  of the perimeter of a polygon. The input is a structure of vertices,  encoded as a list of list

#title.display of vanderwaals isotherm, draw the variation of pressure with...

draw the variation of pressure with volume of a real gas at temperatures lower than its critical temperature.also draw its surface of discontinuity

Program in c to generate random input - python script, For this assignment,...

For this assignment, you are to: 1.    Modify the output format of your Python script from Assignment 1 to match the input format of your C program from Assignment 2. 2.    W

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd