Lecture 10 Data Collections

Slides:



Advertisements
Similar presentations
Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
Advertisements

Guide to Programming with Python
Fundamentals of Python: From First Programs Through Data Structures
Writing Solid Code Introduction to Python. Program 1 python -c "print 'Hello World' “ python -c "import time; print time.asctime()“ Start an interactive.
Data Structures Akshay Singh.  Lists in python can contain any data type  Declaring a list:  a = [‘random’,’variable’, 1, 2]
COMPUTER SCIENCE FEBRUARY 2011 Lists in Python. Introduction to Lists Lists (aka arrays): an ordered set of elements  A compound data type, like strings.
Python Crash Course Containers 3 rd year Bachelors V1.0 dd Hour 3.
Python Data Structures
Lists in Python.
Data Structures in Python By: Christopher Todd. Lists in Python A list is a group of comma-separated values between square brackets. A list is a group.
Python Lists and Such CS 4320, SPRING List Functions len(s) is the length of list s s + t is the concatenation of lists s and t s.append(x) adds.
Beyond Lists: Other Data Structures CS303E: Elements of Computers and Programming.
Built-in Data Structures in Python An Introduction.
Getting Started with Python: Constructs and Pitfalls Sean Deitz Advanced Programming Seminar September 13, 2013.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
Daniel Jung. Types of Data Structures  Lists Stacks Queues  Tuples  Sets  Dictionaries.
Lists CS303E: Elements of Computers and Programming.
14. DICTIONARIES AND SETS Rocky K. C. Chang 17 November 2014 (Based on from Charles Dierbach, Introduction to Computer Science Using Python and Punch and.
Data Collections CS 127. Lists Lists are ordered sequences of items All programming languages provide a sequence structure similar to a Python list; in.
Guide to Programming with Python Chapter Five Lists and dictionaries (data structure); The Hangman Game.
CSC Introduction to Data Structures Devon M. Simmonds Computer Science Department University of North Carolina Wilmington Wilmington, NC 28403
LECTURE 3 Python Basics Part 2. FUNCTIONAL PROGRAMMING TOOLS Last time, we covered function concepts in depth. We also mentioned that Python allows for.
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
LISTS and TUPLES. Topics Sequences Introduction to Lists List Slicing Finding Items in Lists with the in Operator List Methods and Useful Built-in Functions.
Python Programing: An Introduction to Computer Science
Python Data Structures By Greg Felber. Lists An ordered group of items Does not need to be the same type – Could put numbers, strings or donkeys in the.
Advanced Python Idioms
10 - Python Dictionary John R. Woodward.
Python Variable Types.
Lecture 3 Python Basics Part 2.
Algorithmic complexity: Speed of algorithms
Generating Random Numbers
CMSC201 Computer Science I for Majors Lecture 21 – Dictionaries
Data Structures: Lists
Introduction to Python
CS190/295 Programming in Python for Life Sciences: Lecture 5
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Python
Lists in Python.
Bryan Burlingame Halloween 2018
Guide to Programming with Python
CS190/295 Programming in Python for Life Sciences: Lecture 6
8 – Lists and tuples John R. Woodward.
Python Programing: An Introduction to Computer Science
String and Lists Dr. José M. Reyes Álamo.
Introduction to Dictionaries
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
Algorithmic complexity: Speed of algorithms
Topics Sequences Introduction to Lists List Slicing
Python Primer 1: Types and Operators
6. Dictionaries and sets Rocky K. C. Chang 18 October 2018
15-110: Principles of Computing
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Advanced Python Idioms
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CHAPTER 4: Lists, Tuples and Dictionaries
Algorithmic complexity: Speed of algorithms
Advanced Python Idioms
Bryan Burlingame Halloween 2018
Topics Sequences Introduction to Lists List Slicing
And now for something completely different . . .
Lecture 7: Python’s Built-in Types and Basic Statements
Introduction to Dictionaries
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Python Programing: An Introduction to Computer Science
Introduction to Python
CMSC201 Computer Science I for Majors Lecture 19 – Dictionaries
COMPUTER SCIENCE PRESENTATION.
Selamat Datang di “Programming Essentials in Python”
Presentation transcript:

Lecture 10 Data Collections Xiaojuan Cai(蔡小娟) cxj@sjtu.edu.cn Fall, 2015

Objective To be familiar with the functions for manipulating lists. To be able to write programs that use lists to manage a collection of information To be able to write programs that use lists and classes to structure complex data. To understand the use of Python dictionaries for storing nonsequential collections.

Roadmap List basics List sort method Dictionary Example: word frequency

Large data Many programs deal with large collections of similar information. Words in a document Students in a course Data from an experiment Customers of a business Graphics objects drawn on the screen Cards in a deck

Sample: median Let’s review average4.py in lecture 8. It inputs a sequence of numbers and output the average. What if we want to know the median of a sequence of numbers. Median is the data value that splits the data into equal- sized parts. [2, 9, 4, 6, 11]: median is 6, the number in the middle after sorted [2, 9, 6, 4]: median is 5, the average of the two numbers in the middle after sorted.

Sample: median One way to determine the median is to store all the numbers, sort them, and identify the middle value. We need some way to remember these values as they are entered. Lists and arrays!

List Python lists are sequences of items. Almost all computer languages have a sequence structure like this, sometimes called an array. Usually, array is homogeneous (store data with the same type) and fixed size, Python list is heterogeneous (store data with different types) and dynamic (can grow or shrink).

Basic list principles A list is a sequence of items stored as a single object. Items in a list can be accessed by indexing, and sublists can be accessed by slicing. Lists are mutable; individual items or entire slices can be replaced through assignment statements.

List operators (built-in) Meaning <seq> + <seq> Concatenation <seq> * <int-expr> Repetition <seq>[] Indexing len(<seq>) Length <seq>[:] Slicing for <var> in <seq>: Iteration <expr> in <seq> Membership (Boolean)

List operations Method Meaning <list>.append(x) Add element x to end of list. <list>.sort() Sort (order) the list. A comparison function may be passed as a parameter. <list>.reverse() Reverse the list. <list>.index(x) Returns index of first occurrence of x. <list>.insert(i, x) Insert x into list at index i. <list>.count(x) Returns the number of occurrences of x in list. <list>.remove(x) Deletes the first occurrence of x in list. <list>.pop(i) Deletes the ith element of the list and returns its value.

Roadmap List basics List sort method Dictionary Example: word frequency

Sort the list mylist.sort(): return None sorted(mylist): return sorted mylist sort(reverse = True) Lists and classes can express very complex data tyoes. What if we want to sort a list of objects, such as students? Sorting HOW TO: https://docs.python.org/2/howto/sorting.html

Sort the list sort(key = func, reverse = True): new sort(cmp = func, reverse = True) key is a function that given an element returns a value for cmp function. cmp is a function that given two elements return 1,-1,or 0 lambda expression cmp = lambda s,t: cmp(s.getScore(), t.getScore) key = lambda s: s.getScore()

Tuples Tuples are non-mutable lists (like strings), e.g., (1,’a’,3) Indexing and slicing Empty tuple (), one-element tuple (1,). Note that it is not enough to use (1) to represent one-element tuple

Roadmap List basics List sort method Dictionary Example: word frequency

Dictionary In sequences, we look up data by positions, or index. Sometimes, we need more flexible way. For example, retrieve students based on ID number. The data (ID number) we used for indexing is called key, other parts (student info) are called value. A collection associated with arbitrary keys is called a mapping. Python dictionary is a mapping. Other languages have hashes or associative arrays for mapping.

Dictionary basics Syntax: pwd = {“guido”: “supercomputer”, “turing”: “genius”, “bill”: “monopoly”} Keys should be non-mutable built-in data type, such as strings, numbers, tuples, etc. Values can be any data type. Indexing: mydic[key], e.g., pwd[“turing”]

Dictionary is mutable Change value Insert key-value pair pwd[“bill”] = “bluescreen” Insert key-value pair pwd[“zelle”] = “goodwriter” Delete key-value pair del pwd[“turing”] Delete all entries (return an empty dictionary) pwd.clear()

Dictionary operator Method Meaning <dict>.has_key(<key>) Returns true if dictionary contains the key, false otherwise. <key> in <dict> Same as has_key <dict>.keys() Returns a list of keys <dict>.values() Returns a list of values <dict>.items() Returns a list of tuples (key,value), representing the key-value pairs <dcit>.get(<key>,<default>) If dictionary has key returns its value, otherwise, returns default.

Example: word frequency Input: a text document and a number n Output: the first n frequent words in the document Algorithm: use a dictionary to accumulate the frequency of each word. Get items of the dictionary Sort all the items Print the first n pairs

wordfreq.py counts[w] = counts.get(w,0)+1 if w is in the dictionary: add one to the count of w else: set count for w to 1 counts[w] = counts.get(w,0)+1 compareItems((w1,c1),(w2,c2)) counts.items().sort(compareItems)

Conclusion Python lists are very useful data structures. We can use it as other important data structures such as Stack, Queue Python lists are dynamic (heterogeneous, can shrink and grow arbitrarily) Lists and classes can represent complex data. Dictionary is an important non-sequencing data structure. It is also called mapping, hashing, etc..