Python for Informatics: Exploring Information

Slides:



Advertisements
Similar presentations
Python for Informatics: Exploring Information
Advertisements

Container Types in Python
Tuples Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Python for Informatics: Exploring Information
DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.
Loops and Iteration Chapter 5 Python for Informatics: Exploring Information
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 1 Introduction to Computer Programming Chapter 4: For Loops, Strings, and Tuples Michael.
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.
Chapter 7 Lists and Tuples. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Data Structures.
Built-in Data Structures in Python An Introduction.
CIT 590 Intro to Programming Lecture 5 – completing lists.
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
10. Python - Lists The list is a most versatile datatype available in Python, which can be written as a list of comma-separated values (items) between.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
Tuples Chapter 10 Python for Informatics: Exploring Information
Guide to Programming with Python Chapter Five Lists and dictionaries (data structure); The Hangman Game.
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.
Loops and Iteration Chapter 5 Python for Informatics: Exploring Information
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.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Equality, references and mutability COMPSCI 105 SS 2015 Principles of Computer Science.
String and Lists Dr. José M. Reyes Álamo.
Python Dictionaries Chapter 9 Python for Everybody
Python - Lists.
Containers and Lists CIS 40 – Introduction to Programming in Python
CSc 120 Introduction to Computer Programing II
Python Lists Chapter 8 Python for Everybody
Department of Computer Science,
From Think Python How to Think Like a Computer Scientist
Python - Dictionaries.
Week 5: Dictionaries and tuples
Ruth Anderson CSE 140 University of Washington
Tuples Chapter 10 Python for Everybody
LING 388: Computers and Language
CISC101 Reminders Quiz 2 this week.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Bryan Burlingame 03 October 2018
Bryan Burlingame Halloween 2018
CISC101 Reminders Slides have changed from those posted last night…
Guide to Programming with Python
CS190/295 Programming in Python for Life Sciences: Lecture 6
6. Lists Let's Learn Python and Pygame
8 – Lists and tuples John R. Woodward.
Data Structures – 1D Lists
4. sequence data type Rocky K. C. Chang 16 September 2018
String and Lists Dr. José M. Reyes Álamo.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Topics Sequences Introduction to Lists List Slicing
6. Dictionaries and sets Rocky K. C. Chang 18 October 2018
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CISC101 Reminders Assignment 2 due today.
CHAPTER 4: Lists, Tuples and Dictionaries
Python for Informatics: Exploring Information
Ruth Anderson CSE 160 University of Washington
Bryan Burlingame Halloween 2018
Topics Sequences Introduction to Lists List Slicing
CSE 231 Lab 8.
Winter 2019 CISC101 5/26/2019 CISC101 Reminders
For loop Using lists.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Sample lecture slides.
Python - Tuples.
Tuple.
Introduction to Computer Science
Ruth Anderson UW CSE 160 Winter 2016
Presentation transcript:

Python for Informatics: Exploring Information Tuples Chapter 10 Python for Informatics: Exploring Information www.pythonlearn.com

Unless otherwise noted, the content of this course material is licensed under a Creative Commons Attribution 3.0 License. http://creativecommons.org/licenses/by/3.0/. Copyright 2010- Charles Severance

Tuples are like lists Tuples are another kind of sequence that function much like a list - they have elements which are indexed starting at 0 >>> x = ('Glenn', 'Sally', 'Joseph') >>> print x[2]Joseph >>> y = ( 1, 9, 2 ) >>> print y (1, 9, 2) >>> print max(y) 9 >>> for iter in y: ... print iter ... 1 9 2 >>>

..but.. Tuples are "immutable" Unlike a list, once you create a tuple, you cannot alter its contents - similar to a string >>> y = 'ABC’ >>> y[2] = 'D’ Traceback:'str' object does not support item Assignment >>> >>> z = (5, 4, 3)>>> z[2] = 0 Traceback:'tuple' object does not support item Assignment >>> >>> x = [9, 8, 7] >>> x[2] = 6 >>> print x[9, 8, 6] >>>

Things not to do with tuples >>> x = (3, 2, 1) >>> x.sort() Traceback:AttributeError: 'tuple' object has no attribute 'sort’ >>> x.append(5) Traceback:AttributeError: 'tuple' object has no attribute 'append’ >>> x.reverse() Traceback:AttributeError: 'tuple' object has no attribute 'reverse’ >>>

A Tale of Two Sequences >>> l = list() >>> dir(l)[ 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] >>> t = tuple() >>> dir(t) ['count', 'index']

Tuples are more efficient Since Python does not have to build tuple structures to be modifiable, they are simpler and more efficient in terms of memory use and performance than lists So in our program when we are making "temporary variables" we prefer tuples over lists.

Tuples and Assignment We can also put a tuple on the left hand side of an assignment statement We can even omit the parenthesis >>> (x, y) = (4, 'fred') >>> print y Fred >>> (a, b) = (99, 98) >>> print a 99

Tuples and Dictionaries >>> d['csev'] = 2 >>> d['cwen'] = 4 >>> for (k,v) in d.items(): ... print k, v ... csev 2 cwen 4 >>> tups = d.items() >>> print tups [('csev', 2), ('cwen', 4)] The items() method in dictionaries returns a list of (key, value) tuples

Tuples are Comparable The comparison operators work with tuples and other sequences If the first item is equal, Python goes on to the next element, and so on, until it finds elements that differ. >>> (0, 1, 2) < (5, 1, 2) True >>> (0, 1, 2000000) < (0, 3, 4) >>> ( 'Jones', 'Sally' ) < ('Jones', 'Sam') >>> ( 'Jones', 'Sally') > ('Adams', 'Sam')

Sorting Lists of Tuples We can take advantage of the ability to sort a list of tuples to get a sorted version of a dictionary First we sort the dictionary by the key using the items() method >>> d = {'a':10, 'b':1, 'c':22} >>> t = d.items() >>> t [('a', 10), ('c', 22), ('b', 1)] >>> t.sort() [('a', 10), ('b', 1), ('c', 22)]

Using sorted() >>> d = {'a':10, 'b':1, 'c':22} >>> d.items() [('a', 10), ('c', 22), ('b', 1)] >>> t = sorted(d.items()) >>> t [('a', 10), ('b', 1), ('c', 22)] >>> for k, v in sorted(d.items()): ... print k, v ... a 10 b 1 c 22 We can do this even more directly using the built-in function sorted that takes a sequence as a parameter and returns a sorted sequence

Sort by values instead of key If we could construct a list of tuples of the form (value, key) we could sort by value We do this with a for loop that creates a list of tuples >>> c = {'a':10, 'b':1, 'c':22} >>> tmp = list() >>> for k, v in c.items() : ... tmp.append( (v, k) ) ... >>> print tmp [(10, 'a'), (22, 'c'), (1, 'b')] >>> tmp.sort(reverse=True) [(22, 'c'), (10, 'a'), (1, 'b')]

The top 10 most common words. fhand = open('romeo.txt') counts = dict() for line in fhand: words = line.split() for word in words: counts[word] = counts.get(word, 0 ) + 1 lst = list() for key, val in counts.items(): lst.append( (val, key) ) lst.sort(reverse=True) for val, key in lst[:10] : print key, val The top 10 most common words.

Even Shorter Version (adv) >>> c = {'a':10, 'b':1, 'c':22} >>> print sorted( [ (v,k) for k,v in c.items() ] ) [(1, 'b'), (10, 'a'), (22, 'c')] List comprehension creates a dynamic list. In this case, we make a list of reversed tuples and then sort it. http://wiki.python.org/moin/HowTo/Sorting

Summary Tuple syntax Using sorted() Mutability (not) Sorting dictionaries by either key or value Comparability Sortable Tuples in assignment statements