Python Lists. What is not a Collection. A List is a kind of Collection. friends = [ 'Joseph', 'Glenn', 'Sally' ]

Similar documents
It is time to go find some Data to mess with!

Spring INF Principles of Programming for Informatics. Manipulating Lists

Python for Informatics

Reading Files. Chapter 7. Python for Informatics: Exploring Information

Python for Everybody. Exploring Data Using Python 3. Charles R. Severance

Loops and Iteration. Chapter 5. Python for Informatics: Exploring Information

Reading Files. Chapter 7. Python for Everybody

Data Structures. Lists, Tuples, Sets, Dictionaries

Dictionaries, Text, Tuples

CMPT 120 Lists and Strings. Summer 2012 Instructor: Hassan Khosravi

Python - Loops and Iteration

Python workshop. Week 4: Files and lists.

Sequence types. str and bytes are sequence types Sequence types have several operations defined for them. Sequence Types. Python

Strings. Chapter 6. Python for Everybody

Lists in Python CS 8: Introduction to Computer Science, Winter 2018 Lecture #10

The Practice of Computing Using PYTHON

Collections. Lists, Tuples, Sets, Dictionaries

CS2304: Python for Java Programmers. CS2304: Sequences and Collections

Lists, loops and decisions

Programming Fundamentals and Python

Topic 7: Lists, Dictionaries and Strings

Introduction to Python

University of Texas at Arlington, TX, USA

Python for Informatics

1 Strings (Review) CS151: Problem Solving and Programming

In addition to numbers and strings, Python also lets us represent lists of things (just like in AppInventor).

Python Intro GIS Week 1. Jake K. Carr

Lists How lists are like strings

Programming in Python

Iterators & Generators

CPD for GCSE Computing: Practical Sheet 6 February 14

Dictionaries. Looking up English words in the dictionary. Python sequences and collections. Properties of sequences and collections

MITOCW watch?v=rvrkt-jxvko

SI Networked Computing: Storage, Communication, and Processing, Winter 2009

Introduction to Python

The Big Python Guide

Python I. Some material adapted from Upenn cmpe391 slides and other sources

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

Types, lists & functions

Lecture 12. Lists (& Sequences)

At full speed with Python

Accelerating Information Technology Innovation

Slicing. Open pizza_slicer.py

Lecture 7: Python s Built-in. in Types and Basic Statements

CSc 120. Introduc/on to Computer Programing II. 01- b: Python review. Adapted from slides by Dr. Saumya Debray

Data Structures. Dictionaries - stores a series of unsorted key/value pairs that are indexed using the keys and return the value.

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

CPSC 217-T03/T08. Functions Ruting Zhou

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

Introduction to Python 2

CSc 120 Introduction to Computer Programing II Adapted from slides by Dr. Saumya Debray

Sequence Types FEB

Chapter 6: List. 6.1 Definition. What we will learn: What you need to know before: Data types Assignments

Sets and Dictionaries

CSC326 Python Sequences i. CSC326 Python Sequences

Arrays (Lists) # or, = ("first string", "2nd string", 123);

CS Summer 2013

Part I. Wei Tianwen. A Brief Introduction to Python. Part I. Wei Tianwen. Basics. Object Oriented Programming

Compound Data Types 1

Conditional Execution

3.4. FOR-LOOPS 65. for <v a r i a b l e > in < sequence >:

Python for loops. Girls Programming Network School of Information Technologies University of Sydney. Mini-lecture 7

def order(food): food = food.upper() print( Could I have a big + food + please? ) return fresh + food

Computer Sciences 368 Scripting for CHTC Day 3: Collections Suggested reading: Learning Python

18.1. CS 102 Unit 18. Python. Mark Redekopp

Python and Bioinformatics. Pierre Parutto

Stored (and reused) Steps

Program Planning, Data Comparisons, Strings

Accelerating Information Technology Innovation

Worksheet 6: Basic Methods Methods The Format Method Formatting Floats Formatting Different Types Formatting Keywords

Module 04: Lists. Topics: Lists and their methods Mutating lists Abstract list functions Readings: ThinkP 8, 10. CS116 Fall : Lists

Introduction to Problem Solving and Programming in Python.

MUTABLE LISTS AND DICTIONARIES 4

CIS192 Python Programming

Chapter 10: Creating and Modifying Text Lists Modules

1ο Φροντιστήριο Τεχνητής Νοημοσύνης. Διαδικαστικά και εισαγωγή στην Python

A Little Python Part 1. Introducing Programming with Python

CMSC201 Computer Science I for Majors

CS1 Lecture 11 Feb. 9, 2018

Physics 514 Basic Python Intro

Sequences and Loops. Indices: accessing characters in a string. Old friend: isvowel. Motivation: How to count the number of vowels in a word?

Introduction to Python. Data Structures

Unit 2. Srinidhi H Asst Professor

CMPT 120 Basics of Python. Summer 2012 Instructor: Hassan Khosravi

Python: common syntax

CS 1110 Prelim 2 November 14th, 2013

CS 1110: Introduction to Computing Using Python Lists and Sequences

CMSC201 Computer Science I for Majors

CMSC201 Computer Science I for Majors

Python Review IPRE

UNIT 5. String Functions and Random Numbers

Sequences and Loops. Indices: accessing characters in a string. Old friend: isvowel. Motivation: How to count the number of vowels in a word?

CS1 Lecture 12 Feb. 11, 2019

All programs can be represented in terms of sequence, selection and iteration.

Python for Non-programmers

CIS192: Python Programming Data Types & Comprehensions Harry Smith University of Pennsylvania September 6, 2017 Harry Smith (University of Pennsylvani

Script language: Python Data structures

Python Programming Exercises 3

Statements 2. a operator= b a = a operator b

TUPLES AND RECURSIVE LISTS 5

Transcription:

Python Lists Chapter 8 Unless otherwise noted, the content of this course material is licensed under a Creative Commons Attribution.0 License. http://creativecommons.org/licenses/by/.0/. Copyright 2010, Charles Severance Python for Informatics: Exploring Information www.py4inf.com A List is a kind of Collection A collection allows us to put many values in a single variable A collection is nice because we can carry all many values around in one convenient package. friends = [ 'Joseph', 'Glenn', 'Sally' ] carryon = [ "socks", "shirt", "perfume" ] What is not a Collection Most of our variables have one value in them - when we put a new value in the variable - the old value is over written $ python Python 2.5.2 (r252:60911, Feb 22 2008, 07:57:5) [GCC 4.0.1 (Apple Computer, Inc. build 56)] on darwin x = 2 x = 4 print x 4

List Constants We already use lists! List constants are surrounded by square brakets and the elements in the list are separated by commas. A list element can be any Python object - even another list A list can be empty print [1, 24, 76] [1, 24, 76] print ['red', 'yellow', 'blue'] ['red', 'yellow', 'blue'] print ['red', 24, 98.6] ['red', 24, 98.599999999999994] print [ 1, [5, 6], 7] [1, [5, 6], 7] print [] [] for i in [5, 4,, 2, 1] : print i print "Blastoff!" 5 4 2 1 Blastoff! Lists and definite loops - best pals Looking Inside Lists friends = ['Joseph', 'Glenn', 'Sally'] for friend in friends : print 'Happy New Year:', friend print "Done!" Happy New Year: Joseph Happy New Year: Glenn Happy New Year: Sally Done! Just like strings, we can get at any single character in a string using an index specified in square brackets Joseph 0 Glenn 1 Sally 2 friends = [ 'Joseph', 'Glenn', 'Sally' ] print friends[1] Glenn

Lists are Mutable Strings are "immutable" - we cannot change the contents of a string - we must make a new string to make any change Lists are "mutable" - we can change an element of a list using the index operator fruit = 'Bannna' fruit[0] = 'b' Traceback TypeError: 'str' object does not support item assignment x = fruit.lower() print x bannna lotto = [2, 14, 26, 41, 6] print lotto [2, 14, 26, 41, 6] lotto[2] = 28 print lotto [2, 14, 28, 41, 6] How Long is a List? The len() function takes a list as a parameter and returns the number of elements in the list Actually len() tells us the number of elements of any set or sequence (i.e. such as a string...) greet = "Hello Bob" print len(greet) 9 x = [ 1, 2, "fred", 99] print len(x) 4 Using the range function A tale of two loops... The range function returns a set of numbers that range from zero to the length of a set We can construct an index loop using for and an integer iterator friends = ['Joseph', 'Glenn', 'Sally'] print len(friends) print range(len(friends)) [0, 1, 2] friends = ['Joseph', 'Glenn', 'Sally'] for friend in friends : print 'Happy New Year:', friend for i in range(friends) : print 'Happy New Year:', friends[i] friends = ['Joseph', 'Glenn', 'Sally'] print len(friends) print range(len(friends)) [0, 1, 2] Happy New Year: Joseph Happy New Year: Glenn Happy New Year: Sally

Remembering this loop... A more "Pythonic" version found = False where = -1 tmppos = 0 print "Before", found for value in [, 41, 12, 9, 74, 15] : tmppos = tmppos + 1 if value == 9: found = True where = tmppos print found, where, value Before False False -1 False -1 41 False -1 12 True 4 9 True 4 74 True 4 15 After True 4 where = None print "Before", where nums = [, 41, 12, 9, 74, 15] for ind in range(len(nums)) : if nums[ind] == 9: where = ind print where, i, nums[i] print "After", where, nums[where] Before None None 0 None 1 41 None 2 12 9 4 74 5 15 After 9 print "After", found, where Note: We don t print out tmppos at the end Concatenating lists using + Lists can be sliced using the : We can create a new list by adding two exsiting lists together a = [1, 2, ] b = [4, 5, 6] c = a + b print c [1, 2,, 4, 5, 6] print a [1, 2, ] t = ['a', 'b', 'c', 'd', 'e', 'f'] t[1:] ['b', 'c'] t[:4] ['a', 'b', 'c', 'd'] t[:] ['d', 'e', 'f'] t[:] ['a', 'b', 'c', 'd', 'e', 'f']

List Methods Building a list from scratch x = list() type(x) <type 'list'> dir(x) ['append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] http://docs.python.org/tutorial/datastructures.html We can create an empty list and then add elements using the append method The list stays in order and new elements are added at the end of the list stuff = list() stuff.append('book') stuff.append('ipod') print stuff ['book', 'ipod'] stuff.append('cookie') print stuff ['book', 'ipod', 'cookie'] A List is an Ordered Sequence A list can hold many items and keeps those items in the order until we do something to change the order A list can be sorted (i.e. change its order) friends = [ 'Joseph', 'Glenn', 'Sally' ] friends.sort() print friends ['Glenn', 'Joseph', 'Sally'] print friends[2] Sally Deleting items from a list We can pull off the last item of a list using pop() We can del(ete) an item if we know its index position We can also remove an item that matches a particular value t = ['a', 'b', 'c'] x = t.pop() print t ['a','b'] print x c t = ['a', 'b', 'c'] del t[1] print t ['a', 'c'] t = ['a', 'b', 'c'] t.remove('b') print t ['a', 'c']

Built in Functions and Lists There are a number of functions built into Python that take lists as parameters Remember the loops we built? These are much simpler http://docs.python.org/lib/built-in-funcs.html nums = [, 41, 12, 9, 74, 15] print len(nums) 6 print sum(nums) 154 print max(nums) 74 print min(nums) print sum(nums)/len(nums) 25 abc = 'With three words' stuff = abc.split() print stuff ['With', 'three', 'words'] print len(stuff) print stuff[0] With Strings and Lists print stuff ['With', 'three', 'words'] for w in stuff :... print w... With three words Split breaks a string into parts produces a list of strings. We think of these as words. We can access a particular word or loop through all the words. line = 'A lot of spaces' etc = line.split() print etc ['A', 'lot', 'of', 'spaces'] line = 'first,second,third' thing = line.split() print thing ['first,second,third'] print len(thing) 1 thing = line.split(',') print thing ['first', 'second', 'third'] print len(thing) When you do not specify a delimiter, multiple spaces are treated like one delimiter. You can specify what delimiter character to use in the splitting. From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008 fhand = open('mbox-short.txt') for line in fhand: line = line.rstrip() if not line.startswith('from ') : continue words = line.split() print words[2] Sat Fri Fri Fri... line = "From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008" words = line.split() print words ['From', 'stephen.marquard@uct.ac.za', 'Sat', 'Jan', '5', '09:14:16', '2008']

Mystery Problem... List Summary From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008 fhand = open('mbox-short.txt') for line in fhand: line = line.rstrip() words = line.split() if words[0]!= 'From' : continue print words[2] Sat Traceback (most recent call last): File "search8.py", line 5, in <module> if words[0]!= 'From' : continue IndexError: list index out of range Concept of a collection Lists and definite loops Indexing and lookup List mutability Functions: len, min, max, sum Slicing lists List methods: append, remove Sorting lists Splitting strings into lists of words Using split to parse strings