SlideShare a Scribd company logo
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

Hashing 1
Hashing 1Hashing 1
Hashing 1
Shyam Khant
 
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
DlearAhmad
 
DBMS 9 | Extendible Hashing
DBMS 9 | Extendible HashingDBMS 9 | Extendible Hashing
DBMS 9 | Extendible Hashing
Mohammad Imam Hossain
 
Hashing
HashingHashing
Hashing
Dinesh Vujuru
 
Extensible hashing
Extensible hashingExtensible hashing
Extensible hashing
rajshreemuthiah
 
Hashing In Data Structure
Hashing In Data Structure Hashing In Data Structure
Hashing In Data Structure
Meghaj Mallick
 
Math
MathMath
Advance algorithm hashing lec I
Advance algorithm hashing lec IAdvance algorithm hashing lec I
Advance algorithm hashing lec ISajid Marwat
 
Lecture 11
Lecture 11Lecture 11
Lecture 11
talha ijaz
 
Concept of hashing
Concept of hashingConcept of hashing
Concept of hashingRafi Dar
 
Hashing in datastructure
Hashing in datastructureHashing in datastructure
Hashing in datastructure
rajshreemuthiah
 
Hash Tables in data Structure
Hash Tables in data StructureHash Tables in data Structure
Hash Tables in data Structure
Prof 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 tasks
azm13
 
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
 
Rehashing
RehashingRehashing
Rehashing
rajshreemuthiah
 
Hashing and Hash Tables
Hashing and Hash TablesHashing and Hash Tables
Hashing and Hash Tables
adil 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 lecture
Rudy Martinez
 
CS 151 CSV output
CS 151 CSV outputCS 151 CSV output
CS 151 CSV output
Rudy Martinez
 
CS 151 homework2a
CS 151 homework2aCS 151 homework2a
CS 151 homework2a
Rudy Martinez
 
Project3
Project3Project3
Project3
ARVIND SARDAR
 
Mapreduce Algorithms
Mapreduce AlgorithmsMapreduce Algorithms
Mapreduce Algorithms
Amund 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.docx
DIPESH30
 
DSE-complete.pptx
DSE-complete.pptxDSE-complete.pptx
DSE-complete.pptx
RanjithKumar888622
 
DutchMLSchool. Automating Decision Making
DutchMLSchool. Automating Decision MakingDutchMLSchool. Automating Decision Making
DutchMLSchool. Automating Decision Making
BigML, Inc
 
Python-for-Data-Analysis.pdf
Python-for-Data-Analysis.pdfPython-for-Data-Analysis.pdf
Python-for-Data-Analysis.pdf
ssuser598883
 
Twitter Mention Graph - Analytics Project
Twitter Mention Graph - Analytics ProjectTwitter Mention Graph - Analytics Project
Twitter Mention Graph - Analytics Project
Sotiris Baratsas
 
Lecture 9.pptx
Lecture 9.pptxLecture 9.pptx
Lecture 9.pptx
MathewJohnSinoCruz
 
PPT on Data Science Using Python
PPT on Data Science Using PythonPPT on Data Science Using Python
PPT on Data Science Using Python
NishantKumar1179
 
Python-for-Data-Analysis.pptx
Python-for-Data-Analysis.pptxPython-for-Data-Analysis.pptx
Python-for-Data-Analysis.pptx
Sandeep Singh
 
Python for Data Analysis.pdf
Python for Data Analysis.pdfPython for Data Analysis.pdf
Python for Data Analysis.pdf
JulioRecaldeLara1
 
Python-for-Data-Analysis.pptx
Python-for-Data-Analysis.pptxPython-for-Data-Analysis.pptx
Python-for-Data-Analysis.pptx
tangadhurai
 
e_lumley.pdf
e_lumley.pdfe_lumley.pdf
e_lumley.pdf
betsegaw123
 
New Features in Apache Pinot
New Features in Apache PinotNew Features in Apache Pinot
New Features in Apache Pinot
Siddharth Teotia
 
Data manipulation on r
Data manipulation on rData manipulation on r
Data manipulation on r
Abhik Seal
 
CS 151 Standard deviation lecture
CS 151 Standard deviation lectureCS 151 Standard deviation lecture
CS 151 Standard deviation lecture
Rudy 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

CS 151Exploration of python
CS 151Exploration of pythonCS 151Exploration of python
CS 151Exploration of python
Rudy Martinez
 
CS 151 Classes lecture 2
CS 151 Classes lecture 2CS 151 Classes lecture 2
CS 151 Classes lecture 2
Rudy Martinez
 
CS 151 Classes lecture
CS 151 Classes lectureCS 151 Classes lecture
CS 151 Classes lecture
Rudy Martinez
 
CS 151 Midterm review
CS 151 Midterm reviewCS 151 Midterm review
CS 151 Midterm review
Rudy Martinez
 
Cs 151 dictionary writer
Cs 151 dictionary writerCs 151 dictionary writer
Cs 151 dictionary writer
Rudy Martinez
 
CS 151 dictionary objects
CS 151 dictionary objectsCS 151 dictionary objects
CS 151 dictionary objects
Rudy Martinez
 
CS 151 Date time lecture
CS 151 Date time lectureCS 151 Date time lecture
CS 151 Date time lecture
Rudy Martinez
 
CS151 FIle Input and Output
CS151 FIle Input and OutputCS151 FIle Input and Output
CS151 FIle Input and Output
Rudy Martinez
 
CS151 Functions lecture
CS151 Functions lectureCS151 Functions lecture
CS151 Functions lecture
Rudy Martinez
 
Lecture4
Lecture4Lecture4
Lecture4
Rudy Martinez
 
Lecture01
Lecture01Lecture01
Lecture01
Rudy Martinez
 
Lecture02
Lecture02Lecture02
Lecture02
Rudy Martinez
 
Lecture03
Lecture03Lecture03
Lecture03
Rudy Martinez
 
Lecture01
Lecture01Lecture01
Lecture01
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

TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 

Recently uploaded (20)

TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 

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