SlideShare a Scribd company logo
1 of 19
Computer Science 151
An introduction to the art of
computing
Copy of Lists Lecture
Rudy Martinez
Homework 2c is posted
• Create a file (homework2c.py).
• Your program should open the homework2.csv and averagedata.csv data file.
• You may use any data structure you like, however, you should think about how you need to read, access,
and write the data for the statistics you need. You would be wise to utilize code you have already written.
• You will create a yearly Standard Deviation of TMIN and TMAX. You should use the following equations to
compute average ( ̄X) and standard deviation (σ).
• 𝑥 =
𝑥
𝑛
𝑎𝑛𝑑 𝜎 =
𝑥−𝑥 2
𝑛−1
• Write a comma separated value (CSV) file to disk (statistics.csv) that holds the following data:
• Year, avgTmax, avgTmin, StdDevMax, StdDevMin
3/26/2019CS151SP19
Homework 2c
• You will be given code to create a graph, however, you must make sure that your data is in the correct
format to be understood by the graphing code.
• You will write a report on your observations of the data and the outcome, you should use the graph
created in your code. This will be a short one to two paragraphs not to exceed one page on what you
think the data is saying. Approach this as an assignment from the point of view that you are analyzing
this data for a boss or management team.
• What can you generalize about the temperature over the time in the data series?
• What kind of temperatures can you generalize for the next 10, 20, 30 years given this data and the standard
deviation?
• Does the data make sense in terms of what you know about Albuquerque’s weather?
• You will turn in your homework2c.py file on learn, you should also upload a pdf of your report
observations
3/26/2019CS151SP19
Python and Lists
• Python tries to be smart with
managing memory.
• Lists can be long so why make a
complete copy?
myList = [ 1, 2, 3, 4, 5]
myCopy = myList
print(‘myList’, myList)
print(‘myCopy’, myCopy)
---------------------------------------
myList 1 2 3 4 5
myCopy 1 2 3 4 5
3/26/2019CS151SP19
Python and Lists
• Now we see that python only
creates a pointer as a ‘copy’
• How do we know this?
myList = [ 1, 2, 3, 4, 5]
myCopy = myList
myList.append(6)
print(‘myList’, myList)
print(‘myCopy’, myCopy)
-------------------------------------------
myList 1 2 3 4 5 6
myCopy 1 2 3 4 5 6
3/26/2019CS151SP19
Enter Deep Copy
• How do we create a real copy then?
import copy
newList = copy.deepcopy(list1)
• The above code will copy the list element by element and place
it into a whole new list
3/26/2019CS151SP19
Enter Deep Copy
• Now we see that python only
creates a pointer as a ‘copy’
• How do we know this?
myList = [ 1, 2, 3, 4, 5]
myrealCopy = copy.deepcopy(myList)
myList.append(6)
print(‘myList’, myList)
print(‘myrealCopy’, myrealCopy)
-------------------------------------------
myList 1 2 3 4 5 6
myCopy 1 2 3 4 5
3/26/2019CS151SP19
Why does this matter?
• If you have a for loop for adding items to a list and you push this
to a new list or dictionary you get a pointer of a copy to the list.
3/26/2019CS151SP19
Example
myList = [ 1, 2, 3, 4, 5]
myDictionary = {}
for i in range(0, len(myList), 1):
myDictionary[str(i)] = myList
3/26/2019CS151SP19
Example
myList = [ 1, 2, 3, 4, 5]
myDictionary = {}
for i in range(0, len(myList), 1):
myDictionary[str(i)] = myList
3/26/2019CS151SP19
This is what you might think is happening.
Example
myList = [ 1, 2, 3, 4, 5]
myDictionary = {}
for i in range(0, len(myList), 1):
myDictionary[str(i)] = myList
3/26/2019CS151SP19
This is what really happens.
Example
myList = [ 1, 2, 3, 4, 5]
myDictionary = {}
for i in range(0, len(myList), 1):
myDictionary[str(i)] = myList
# Add one to myList
myList.append(6)
3/26/2019CS151SP19
This is what really happens.
Example fixed!
myList = [ 1, 2, 3, 4, 5]
myDictionary = {}
for i in range(0, len(myList), 1):
myDictionary[str(i)]=copy.deepcopy(myList)
# Add one to myList
myList.append(6)
3/26/2019CS151SP19
This is what really happens.
Calculating standard deviation
• You already have the list of averages ( 𝑥) in averages.csv
• You will need to get ‘x’ from your cleaned data in homework2.csv
• Then like calculating the sum to get an average you will need to sort
by year.
• You could copy your for loop that did the summation, except you need to
figure out how to store the calculation
𝜎 =
𝑥 − 𝑥 2
𝑛 − 1
3/26/2019CS151SP19
This is the where deepcopy comes in
• In the for loop you can calculate 𝑥 − 𝑥 2
sum += math.pow((myList[i][1] – myAvg),2)
• To square a number use math.pow(number,2)
• To cube change 2 to 3
• myList[i][1]
• If myList is loaded of the original file from homework2.csv
• Year,Tmax, tmin
• myAvg
• You already have averages from homework2b, averages.csv
3/26/2019CS151SP19
This is the where deepcopy comes in
• Load Averages into dictionary
• Year: [Tmax, Tmin]
• Load homework2.csv into list
• Year, tmax, tmin
• Create dictionary to hold Year and a list of the temps for that day (you need two, tmax and tmin)
• Year: [maxTemplist]
• 1994:['68', '38', '67’, …, '25’]
• 1995: ['48', '25', '51’, …, '16’]
• Now you can calculate standard deviation per year
• More on this later
3/26/2019CS151SP19
Graphing
3/26/2019CS151SP19
Code for graphing
This code will be provided to you, however, you will need to have your data in the correct format*:
The Plot takes two lists and plots them.
plt.plot(yearList, maxTempList, label='TMax')
plt.plot(yearList, minTempList, label='TMin')
plt.plot(yearList, plotAvgMax, label='Max Average')
plt.plot(yearList, plotAvgMin, label='Min Average’)
yearList is just a list of the years from the data file.
maxTempList, minTempList is a list of values from homework2b or the averages.csv
plotAvgMax, plotAvgMin is a list of one single value, however the plotter wants a 1:1 so it’s
repeated 25 times.
* Right now this is the format it may slightly change
3/26/2019CS151SP19
List Comprehensions
new_list = [expression for_loop_one_or_more condtions]
# We can use len(avgDict) here because we cleaned the data
avgMax = sumMax/len(avgDict)
avgMin = sumMin/len(avgDict)
# Syntactic Sugar for creating an array of values set to one value
plotAvgMax = [avgMax for i in range(len(avgDict))]
plotAvgMin = [avgMin for i in range(len(avgDict))]
We take a value avgMax and apply it in a for loop from I to length of avgDict.
plotAvgMax = [74.3993317088547, … , 74.3993317088547]
3/26/2019CS151SP19

More Related Content

What's hot

Trigonometric, hyperbolic functions with invers and sum function
Trigonometric, hyperbolic functions with invers and sum functionTrigonometric, hyperbolic functions with invers and sum function
Trigonometric, hyperbolic functions with invers and sum functionDlearAhmad
 
Hashing In Data Structure
Hashing In Data Structure Hashing In Data Structure
Hashing In Data Structure Meghaj Mallick
 
Advance algorithm hashing lec I
Advance algorithm hashing lec IAdvance algorithm hashing lec I
Advance algorithm hashing lec ISajid Marwat
 
Concept of hashing
Concept of hashingConcept of hashing
Concept of hashingRafi Dar
 
Hashing in datastructure
Hashing in datastructureHashing in datastructure
Hashing in datastructurerajshreemuthiah
 
Hash Tables in data Structure
Hash Tables in data StructureHash Tables in data Structure
Hash Tables in data StructureProf Ansari
 
Using information theory principles to schedule real time tasks
 Using information theory principles to schedule real time tasks Using information theory principles to schedule real time tasks
Using information theory principles to schedule real time tasksazm13
 
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Hashing notes data structures (HASHING AND HASH FUNCTIONS)Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Hashing notes data structures (HASHING AND HASH FUNCTIONS)Kuntal Bhowmick
 
Hashing and Hash Tables
Hashing and Hash TablesHashing and Hash Tables
Hashing and Hash Tablesadil raja
 

What's hot (20)

Hashing 1
Hashing 1Hashing 1
Hashing 1
 
Trigonometric, hyperbolic functions with invers and sum function
Trigonometric, hyperbolic functions with invers and sum functionTrigonometric, hyperbolic functions with invers and sum function
Trigonometric, hyperbolic functions with invers and sum function
 
DBMS 9 | Extendible Hashing
DBMS 9 | Extendible HashingDBMS 9 | Extendible Hashing
DBMS 9 | Extendible Hashing
 
Hashing
HashingHashing
Hashing
 
Extensible hashing
Extensible hashingExtensible hashing
Extensible hashing
 
Hashing In Data Structure
Hashing In Data Structure Hashing In Data Structure
Hashing In Data Structure
 
Math
MathMath
Math
 
Advance algorithm hashing lec I
Advance algorithm hashing lec IAdvance algorithm hashing lec I
Advance algorithm hashing lec I
 
Ch17 Hashing
Ch17 HashingCh17 Hashing
Ch17 Hashing
 
Lecture 11
Lecture 11Lecture 11
Lecture 11
 
Concept of hashing
Concept of hashingConcept of hashing
Concept of hashing
 
Hashing in datastructure
Hashing in datastructureHashing in datastructure
Hashing in datastructure
 
Week3 binary trees
Week3 binary treesWeek3 binary trees
Week3 binary trees
 
Hash Tables in data Structure
Hash Tables in data StructureHash Tables in data Structure
Hash Tables in data Structure
 
Using information theory principles to schedule real time tasks
 Using information theory principles to schedule real time tasks Using information theory principles to schedule real time tasks
Using information theory principles to schedule real time tasks
 
Hashing
HashingHashing
Hashing
 
Hashing
HashingHashing
Hashing
 
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Hashing notes data structures (HASHING AND HASH FUNCTIONS)Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
 
Rehashing
RehashingRehashing
Rehashing
 
Hashing and Hash Tables
Hashing and Hash TablesHashing and Hash Tables
Hashing and Hash Tables
 

Similar to CS151 Deep copy

CS 151 Graphing lecture
CS 151 Graphing lectureCS 151 Graphing lecture
CS 151 Graphing lectureRudy Martinez
 
Mapreduce Algorithms
Mapreduce AlgorithmsMapreduce Algorithms
Mapreduce AlgorithmsAmund Tveit
 
Lab 3 Set Working Directory, Scatterplots and Introduction to.docx
Lab 3 Set Working Directory, Scatterplots and Introduction to.docxLab 3 Set Working Directory, Scatterplots and Introduction to.docx
Lab 3 Set Working Directory, Scatterplots and Introduction to.docxDIPESH30
 
DutchMLSchool. Automating Decision Making
DutchMLSchool. Automating Decision MakingDutchMLSchool. Automating Decision Making
DutchMLSchool. Automating Decision MakingBigML, Inc
 
Python-for-Data-Analysis.pdf
Python-for-Data-Analysis.pdfPython-for-Data-Analysis.pdf
Python-for-Data-Analysis.pdfssuser598883
 
Twitter Mention Graph - Analytics Project
Twitter Mention Graph - Analytics ProjectTwitter Mention Graph - Analytics Project
Twitter Mention Graph - Analytics ProjectSotiris Baratsas
 
PPT on Data Science Using Python
PPT on Data Science Using PythonPPT on Data Science Using Python
PPT on Data Science Using PythonNishantKumar1179
 
Python-for-Data-Analysis.pptx
Python-for-Data-Analysis.pptxPython-for-Data-Analysis.pptx
Python-for-Data-Analysis.pptxSandeep Singh
 
Python for Data Analysis.pdf
Python for Data Analysis.pdfPython for Data Analysis.pdf
Python for Data Analysis.pdfJulioRecaldeLara1
 
Python-for-Data-Analysis.pptx
Python-for-Data-Analysis.pptxPython-for-Data-Analysis.pptx
Python-for-Data-Analysis.pptxtangadhurai
 
New Features in Apache Pinot
New Features in Apache PinotNew Features in Apache Pinot
New Features in Apache PinotSiddharth Teotia
 
Data manipulation on r
Data manipulation on rData manipulation on r
Data manipulation on rAbhik Seal
 
CS 151 Standard deviation lecture
CS 151 Standard deviation lectureCS 151 Standard deviation lecture
CS 151 Standard deviation lectureRudy Martinez
 

Similar to CS151 Deep copy (20)

CS 151 Graphing lecture
CS 151 Graphing lectureCS 151 Graphing lecture
CS 151 Graphing lecture
 
CS 151 CSV output
CS 151 CSV outputCS 151 CSV output
CS 151 CSV output
 
CS 151 homework2a
CS 151 homework2aCS 151 homework2a
CS 151 homework2a
 
Project3
Project3Project3
Project3
 
Mapreduce Algorithms
Mapreduce AlgorithmsMapreduce Algorithms
Mapreduce Algorithms
 
Lab 3 Set Working Directory, Scatterplots and Introduction to.docx
Lab 3 Set Working Directory, Scatterplots and Introduction to.docxLab 3 Set Working Directory, Scatterplots and Introduction to.docx
Lab 3 Set Working Directory, Scatterplots and Introduction to.docx
 
DSE-complete.pptx
DSE-complete.pptxDSE-complete.pptx
DSE-complete.pptx
 
DutchMLSchool. Automating Decision Making
DutchMLSchool. Automating Decision MakingDutchMLSchool. Automating Decision Making
DutchMLSchool. Automating Decision Making
 
Python-for-Data-Analysis.pdf
Python-for-Data-Analysis.pdfPython-for-Data-Analysis.pdf
Python-for-Data-Analysis.pdf
 
Twitter Mention Graph - Analytics Project
Twitter Mention Graph - Analytics ProjectTwitter Mention Graph - Analytics Project
Twitter Mention Graph - Analytics Project
 
Lecture 9.pptx
Lecture 9.pptxLecture 9.pptx
Lecture 9.pptx
 
PPT on Data Science Using Python
PPT on Data Science Using PythonPPT on Data Science Using Python
PPT on Data Science Using Python
 
Python-for-Data-Analysis.pptx
Python-for-Data-Analysis.pptxPython-for-Data-Analysis.pptx
Python-for-Data-Analysis.pptx
 
Python for Data Analysis.pdf
Python for Data Analysis.pdfPython for Data Analysis.pdf
Python for Data Analysis.pdf
 
Python-for-Data-Analysis.pptx
Python-for-Data-Analysis.pptxPython-for-Data-Analysis.pptx
Python-for-Data-Analysis.pptx
 
e_lumley.pdf
e_lumley.pdfe_lumley.pdf
e_lumley.pdf
 
Leip103
Leip103Leip103
Leip103
 
New Features in Apache Pinot
New Features in Apache PinotNew Features in Apache Pinot
New Features in Apache Pinot
 
Data manipulation on r
Data manipulation on rData manipulation on r
Data manipulation on r
 
CS 151 Standard deviation lecture
CS 151 Standard deviation lectureCS 151 Standard deviation lecture
CS 151 Standard deviation lecture
 

More from Rudy Martinez

More from Rudy Martinez (14)

CS 151Exploration of python
CS 151Exploration of pythonCS 151Exploration of python
CS 151Exploration of python
 
CS 151 Classes lecture 2
CS 151 Classes lecture 2CS 151 Classes lecture 2
CS 151 Classes lecture 2
 
CS 151 Classes lecture
CS 151 Classes lectureCS 151 Classes lecture
CS 151 Classes lecture
 
CS 151 Midterm review
CS 151 Midterm reviewCS 151 Midterm review
CS 151 Midterm review
 
Cs 151 dictionary writer
Cs 151 dictionary writerCs 151 dictionary writer
Cs 151 dictionary writer
 
CS 151 dictionary objects
CS 151 dictionary objectsCS 151 dictionary objects
CS 151 dictionary objects
 
CS 151 Date time lecture
CS 151 Date time lectureCS 151 Date time lecture
CS 151 Date time lecture
 
CS151 FIle Input and Output
CS151 FIle Input and OutputCS151 FIle Input and Output
CS151 FIle Input and Output
 
CS151 Functions lecture
CS151 Functions lectureCS151 Functions lecture
CS151 Functions lecture
 
Lecture4
Lecture4Lecture4
Lecture4
 
Lecture01
Lecture01Lecture01
Lecture01
 
Lecture02
Lecture02Lecture02
Lecture02
 
Lecture03
Lecture03Lecture03
Lecture03
 
Lecture01
Lecture01Lecture01
Lecture01
 

Recently uploaded

Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 

Recently uploaded (20)

Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 

CS151 Deep copy

  • 1. Computer Science 151 An introduction to the art of computing Copy of Lists Lecture Rudy Martinez
  • 2. Homework 2c is posted • Create a file (homework2c.py). • Your program should open the homework2.csv and averagedata.csv data file. • You may use any data structure you like, however, you should think about how you need to read, access, and write the data for the statistics you need. You would be wise to utilize code you have already written. • You will create a yearly Standard Deviation of TMIN and TMAX. You should use the following equations to compute average ( ̄X) and standard deviation (σ). • 𝑥 = 𝑥 𝑛 𝑎𝑛𝑑 𝜎 = 𝑥−𝑥 2 𝑛−1 • Write a comma separated value (CSV) file to disk (statistics.csv) that holds the following data: • Year, avgTmax, avgTmin, StdDevMax, StdDevMin 3/26/2019CS151SP19
  • 3. Homework 2c • You will be given code to create a graph, however, you must make sure that your data is in the correct format to be understood by the graphing code. • You will write a report on your observations of the data and the outcome, you should use the graph created in your code. This will be a short one to two paragraphs not to exceed one page on what you think the data is saying. Approach this as an assignment from the point of view that you are analyzing this data for a boss or management team. • What can you generalize about the temperature over the time in the data series? • What kind of temperatures can you generalize for the next 10, 20, 30 years given this data and the standard deviation? • Does the data make sense in terms of what you know about Albuquerque’s weather? • You will turn in your homework2c.py file on learn, you should also upload a pdf of your report observations 3/26/2019CS151SP19
  • 4. Python and Lists • Python tries to be smart with managing memory. • Lists can be long so why make a complete copy? myList = [ 1, 2, 3, 4, 5] myCopy = myList print(‘myList’, myList) print(‘myCopy’, myCopy) --------------------------------------- myList 1 2 3 4 5 myCopy 1 2 3 4 5 3/26/2019CS151SP19
  • 5. Python and Lists • Now we see that python only creates a pointer as a ‘copy’ • How do we know this? myList = [ 1, 2, 3, 4, 5] myCopy = myList myList.append(6) print(‘myList’, myList) print(‘myCopy’, myCopy) ------------------------------------------- myList 1 2 3 4 5 6 myCopy 1 2 3 4 5 6 3/26/2019CS151SP19
  • 6. Enter Deep Copy • How do we create a real copy then? import copy newList = copy.deepcopy(list1) • The above code will copy the list element by element and place it into a whole new list 3/26/2019CS151SP19
  • 7. Enter Deep Copy • Now we see that python only creates a pointer as a ‘copy’ • How do we know this? myList = [ 1, 2, 3, 4, 5] myrealCopy = copy.deepcopy(myList) myList.append(6) print(‘myList’, myList) print(‘myrealCopy’, myrealCopy) ------------------------------------------- myList 1 2 3 4 5 6 myCopy 1 2 3 4 5 3/26/2019CS151SP19
  • 8. Why does this matter? • If you have a for loop for adding items to a list and you push this to a new list or dictionary you get a pointer of a copy to the list. 3/26/2019CS151SP19
  • 9. Example myList = [ 1, 2, 3, 4, 5] myDictionary = {} for i in range(0, len(myList), 1): myDictionary[str(i)] = myList 3/26/2019CS151SP19
  • 10. Example myList = [ 1, 2, 3, 4, 5] myDictionary = {} for i in range(0, len(myList), 1): myDictionary[str(i)] = myList 3/26/2019CS151SP19 This is what you might think is happening.
  • 11. Example myList = [ 1, 2, 3, 4, 5] myDictionary = {} for i in range(0, len(myList), 1): myDictionary[str(i)] = myList 3/26/2019CS151SP19 This is what really happens.
  • 12. Example myList = [ 1, 2, 3, 4, 5] myDictionary = {} for i in range(0, len(myList), 1): myDictionary[str(i)] = myList # Add one to myList myList.append(6) 3/26/2019CS151SP19 This is what really happens.
  • 13. Example fixed! myList = [ 1, 2, 3, 4, 5] myDictionary = {} for i in range(0, len(myList), 1): myDictionary[str(i)]=copy.deepcopy(myList) # Add one to myList myList.append(6) 3/26/2019CS151SP19 This is what really happens.
  • 14. Calculating standard deviation • You already have the list of averages ( 𝑥) in averages.csv • You will need to get ‘x’ from your cleaned data in homework2.csv • Then like calculating the sum to get an average you will need to sort by year. • You could copy your for loop that did the summation, except you need to figure out how to store the calculation 𝜎 = 𝑥 − 𝑥 2 𝑛 − 1 3/26/2019CS151SP19
  • 15. This is the where deepcopy comes in • In the for loop you can calculate 𝑥 − 𝑥 2 sum += math.pow((myList[i][1] – myAvg),2) • To square a number use math.pow(number,2) • To cube change 2 to 3 • myList[i][1] • If myList is loaded of the original file from homework2.csv • Year,Tmax, tmin • myAvg • You already have averages from homework2b, averages.csv 3/26/2019CS151SP19
  • 16. This is the where deepcopy comes in • Load Averages into dictionary • Year: [Tmax, Tmin] • Load homework2.csv into list • Year, tmax, tmin • Create dictionary to hold Year and a list of the temps for that day (you need two, tmax and tmin) • Year: [maxTemplist] • 1994:['68', '38', '67’, …, '25’] • 1995: ['48', '25', '51’, …, '16’] • Now you can calculate standard deviation per year • More on this later 3/26/2019CS151SP19
  • 18. Code for graphing This code will be provided to you, however, you will need to have your data in the correct format*: The Plot takes two lists and plots them. plt.plot(yearList, maxTempList, label='TMax') plt.plot(yearList, minTempList, label='TMin') plt.plot(yearList, plotAvgMax, label='Max Average') plt.plot(yearList, plotAvgMin, label='Min Average’) yearList is just a list of the years from the data file. maxTempList, minTempList is a list of values from homework2b or the averages.csv plotAvgMax, plotAvgMin is a list of one single value, however the plotter wants a 1:1 so it’s repeated 25 times. * Right now this is the format it may slightly change 3/26/2019CS151SP19
  • 19. List Comprehensions new_list = [expression for_loop_one_or_more condtions] # We can use len(avgDict) here because we cleaned the data avgMax = sumMax/len(avgDict) avgMin = sumMin/len(avgDict) # Syntactic Sugar for creating an array of values set to one value plotAvgMax = [avgMax for i in range(len(avgDict))] plotAvgMin = [avgMin for i in range(len(avgDict))] We take a value avgMax and apply it in a for loop from I to length of avgDict. plotAvgMax = [74.3993317088547, … , 74.3993317088547] 3/26/2019CS151SP19