Python Advanced Data Structures

Slides:



Advertisements
Similar presentations
Introduction to Programming Lecture 39. Copy Constructor.
Advertisements

Ruby on Rails Tutorial Peter Mosca April, Ruby on Rails Tutorial Ruby History Invented 12 years ago in Japan by Yukihiro Matsumoto Spent first 5.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
Sorting. Simple Sorting As you are probably aware, there are many different sorting algorithms: selection sort, insertion sort, bubble sort, heap sort,
Excel. BCSII-5: The student will utilize spreadsheet software a) Identify uses of spreadsheet software and careers related to spreadsheet. b) Identify.
Lists in Python.
Perl Tutorial Presented by Pradeepsunder. Why PERL ???  Practical extraction and report language  Similar to shell script but lot easier and more powerful.
1.NET Web Forms Business Forms © 2002 by Jerry Post.
Targil 6 Notes This week: –Linear time Sort – continue: Radix Sort Some Cormen Questions –Sparse Matrix representation & usage. Bucket sort Counting sort.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Recap form last time How to do for loops map, filter, reduce Next up: dictionaries.
Arrays and Strings. Why? Consider a class of 30 students, each has a score for hw1  Do we want to have 30 variables with different names?  Would it.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Upgrading to EasyGrade Pro Version 4 (EGPv4) Same Easy to Use GradeBook With Enhanced Features.
Guide to Programming with Python Chapter Five Lists and dictionaries (data structure); The Hangman Game.
Peeking at Programming with JavaScript. So what’s JavaScript ? A programming language built into your Web Browser program. Adds fun and interactivity.
Matrix Multiplication The Introduction. Look at the matrix sizes.
Notes Over 4.2 Finding the Product of Two Matrices Find the product. If it is not defined, state the reason. To multiply matrices, the number of columns.
Python Lists and Sequences Peter Wad Sackett. 2DTU Systems Biology, Technical University of Denmark List properties What are lists? A list is a mutable.
Python Sets and Dictionaries Peter Wad Sackett. 2DTU Systems Biology, Technical University of Denmark Set properties What is a set? A set is a mutable.
Today… Files from the Web! Dictionaries. Lists of lists. Winter 2016CISC101 - Prof. McLeod1.
Dictionaries Alexandra Stefan CSE1310 – University of Texas at Arlington.
Python Advanced Data Structures Peter Wad Sackett.
Python Simple file reading Peter Wad Sackett. 2DTU Systems Biology, Technical University of Denmark Simple Pythonic file reading Python has a special.
CMSC201 Computer Science I for Majors Lecture 17 – Dictionaries
CMSC201 Computer Science I for Majors Lecture 21 – Dictionaries
When to use Tuples instead of Lists
Do it now activity Last lesson we used Flowol to create a solution to a problem a computer could solve. Identify what each symbol does:
Python Comprehension and Generators
CMSC201 Computer Science I for Majors Lecture 12 – Lists (cont)
Department of Computer Science,
Matrix 2015/11/18 Hongfei Yan zip(*a) is matrix transposition
COMP280:Introduction to Software Development Week 12, Lecture 34
CSC113: Computer Programming (Theory = 03, Lab = 01)
Python Useful Functions and Methods
Matrix 2016/11/30 Hongfei Yan zip(*a) is matrix transposition
CSC113: Computer Programming (Theory = 03, Lab = 01)
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Introduction to MATLAB
For -G7 programing language Teacher / Shamsa Hassan Alhassouni.
More Loop Examples Functions and Parameters
Python I/O.
File Handling Programming Guides.
Guide to Programming with Python
Introduction to .NetTiers
Python Useful Functions and Methods
Shadows Shadows CSCI 440 textbook section 5.10.
Lists Part 1 Taken from notes by Dr. Neil Moore
Python Lists and Sequences
ARRAYS 2 GCSE COMPUTER SCIENCE.
Python Comprehension and Generators
Python Sets and Dictionaries
Simulation And Modeling
What is x? x = 12 %
15-110: Principles of Computing
Dimensions matching Rows times Columns
And now for something completely different . . .
Introduction to Computer Science
2-Dimensional Lists (Matrices) in Python
Python Review
2d Arrays.
HOW TO COPY AND PASTE THE SNAP CUBES.
Python Inputs Mr. Husch.
Winter 2019 CISC101 5/26/2019 CISC101 Reminders
Python Simple file reading
Python Useful Functions and Methods
CMSC201 Computer Science I for Majors Lecture 19 – Dictionaries
Sample lecture slides.
Dictionary.
Presentation transcript:

Python Advanced Data Structures Peter Wad Sackett

Designing the data structure to fit your need Modeling reality By designing the right data structure for the problem the code becomes much easier to create. Python’s object model makes it easy to create any data structure. A data object can be almost any place where a simple value can be. Simple values are strings and numbers. More complex objects are lists, dicts and sets. For example lists can be inside lists, similar to the way for loops can be inside for loops.

Lists of lists Creating a list of lists, intuitive use is matrix Accessing a row and a single number print(matrix[1], matrix[2][2]) matrix[0][0] = 0 Adding a row matrix += [[13, 14, 15]] Adding a column (list must match the matrix height) for number in enumerate([3, 6, 9, 12, 15]): matrix[number[0]].append(number[1]) Printing the numbers in the matrix – one per line for row in range(len(matrix)): for column in range(len(matrix[row])): print(matrix[row][column])

Dicts of dicts The king of flexibility – example of protein-protein interactions ppi = {”prot_a”: {”prot_b”: ”strong”, ”prot_c”: ”weak”}, ”prot_b”: {”prot_a”: ”strong”, ”prot_c”: ”none”}, ”prot_c”: {”prot_a”: ”weak”, ”prot_b”: ”none”}} print(ppi[”prot_b”], ppi[”prot_b”][”prot_a”]) Adding a primary key/value pair ppi[”prot_x”] = {”prot_a”: ”none”, ”prot_b”: ”strong”} or ppi[”prot_x”] = dict() ppi[”prot_x”][”prot_a”] = ”none” ppi[”prot_x”][”prot_b”] = ”strong” Iterating over dict of dicts for priKey in ppi: for secKey in ppi[priKey]: print(priKey, secKey, ppi[priKey][secKey])

Dicts of lists An example could be to hold people’s scores scores = {’Ann’: [7, 10, 4, 2], ’Dave’: [2, 7, 7, 10]} Accessing all of Ann’s and a single of Dave’s scores print(scores[’Ann’], scores[’Dave’][2]) Adding a new person and a single score for Ann scores[’Mia’] = [4, 4, 2, 2] scores[’Ann’] += [7] Iteration for person in scores: for score in scores[person]: print(person, score)

A more interesting and diverse structure - human Designing an extendable data structure that can model a human. human = { ’firstName’: ’Peter’, ’lastName’: ’Sackett’, ’children’: [’Marcus’, ’Freya’], ’jobs’: {’web designer’: [1995, 2000], ’teacher’: [2000, 2016]} } The structure can be a irregular as wanted/needed. Imagine a list of these. Adding a job. human[’jobs’][’president’] = [2020, 2025]

Danger – copying advanced data structures Whenever you copy an advanced data structure, you make a shallow copy. Consider if you need a deep copy instead (real copy).

Deep copy vs shallow copy Making copies in python, using the library. import copy mat = [[1,2,3],[4,5,6],[7,8,9]] # making a shallow copy of mat, existing rows are shared shallow_mat = mat # or shallow2_mat = copy.copy(mat) # making a deep copy of mat, nothing is shared deep_mat = copy.deepcopy(mat) Shallow copy pros: Quick, uses much less memory. Shallow copy cons: Changes in the copy are reflected in the original. Warning: Many programs have failed in strange ways, because the data sharing was not considered.