Data Collections CS 127. Lists Lists are ordered sequences of items All programming languages provide a sequence structure similar to a Python list; in.

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Container Types in Python
Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
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.
Guide to Programming with Python
Fundamentals of Python: From First Programs Through Data Structures
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.
CS107 Introduction to Computer Science Lecture 5, 6 An Introduction to Algorithms: List variables.
Lists/Tuples. Example Write a program to keep track of all students’ scores on exam 1. Need a list of everyone’s score Use 14 doubles What about next.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 6: Lists, Tuples, and Dictionaries – Exercises Xiang Lian The University of Texas – Pan American Edinburg,
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
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 Data Structures
Lists in Python.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 14 Tuples, Sets, and Dictionaries 1.
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.
Python Programming in Context Chapter 4. Objectives To understand Python lists To use lists as a means of storing data To use dictionaries to store associative.
Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Built-in Data Structures in Python An Introduction.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 8 Working.
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
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.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 Dictionaries and Sets.
Lists CS303E: Elements of Computers and Programming.
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
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.
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
Guide to Programming with Python Chapter Five Lists and dictionaries (data structure); The Hangman Game.
Chapter 10 Loops: while and for CSC1310 Fall 2009.
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
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
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.
CSc 110, Spring 2017 Lecture 29: Sets and Dictionaries
Generating Random Numbers
Lecture 10 Data Collections
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CSc 110, Spring 2018 Lecture 32: Sets and Dictionaries
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Introduction to Python
Lists in Python.
CHAPTER THREE Sequences.
Guide to Programming with Python
Chapter 10 Lists.
CS190/295 Programming in Python for Life Sciences: Lecture 6
4. sequence data type Rocky K. C. Chang 16 September 2018
Python Programing: An Introduction to Computer Science
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Chapter 5: Lists and Dictionaries
Topics Sequences Introduction to Lists List Slicing
Dictionaries Dictionary: object that stores a collection of data
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CISC101 Reminders Assignment 2 due today.
Topics Sequences Introduction to Lists List Slicing
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Python Programing: An Introduction to Computer Science
Introduction to Python
Dictionary.
Introduction to Computer Science
Presentation transcript:

Data Collections CS 127

Lists Lists are ordered sequences of items All programming languages provide a sequence structure similar to a Python list; in other languages it is called an array A list (or array) is a sequence of items where the entire sequence is referred to by a single name and the individual items can be selected by indexing sum = 0 for i in range(10): sum = sum +i

Lists Arrays are usually homogeneous (they are restricted to storing objects of a single data type) Arrays in other languages generally have a fixed size, which is specified when you create an array Python lists are dynamic - they can grow and shrink on demand Python lists are also heterogenous - Arbitrary data types can be mixed in a single list Python lists are mutable sequences of arbitrary objects

List Operations OperatorMeaning + Concatenation * Repitition []Indexing [:]Slicing len( )Length for in Iteration in Membership check (returns a Boolean)

Membership check >>> myList = [1,2,3,4] >>> 3 in myList True >>> 5 in myList False >>> ans =‘Y’ >>>ans in ‘Yy’ True

List Operation Examples >>> myList = [1,2,3,4] >>> myList[3] 4 >>>myList[3]=‘Hello’ >>> myList [1, 2, 3, ‘Hello’] >>> myList[2]=7 >>>myList [1, 2, 7, ‘Hello’] >>> myList[1:3] = [“Slice”,”Assignment”] >>>myList >>>[1,’Slice’,’Assignment’,’Hello’]

Manipulating Lists OperatorMeaning.append(x)Add element x to end of list.sort()Sort (order) the list.reverse()Reverse the list.index(x) Returns index of first occurrence of x.insert(i, x)Insert x into list at index i.count(x) Returns the number of occurrences of x in list.remove(x) Deletes the first occurrence of x in list.pop(i) Deletes the ith element of the list and returns its value

Deleting items from lists >>> myList [34, 26, 0, 10] >>>del myList[1] >>> myList [34, 0, 10] >>> del myList[1:3] >>>myList [34] del is not a list method. It is a built-in operation that can be used on list items

List Summary 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 Lists support a number of convenient and frequently used methods Lists will grow and shrink as needed

Lists Arrays are usually homogeneous (they are restricted to storing objects of a single data type) Arrays in other languages generally have a fixed size, which is specified when you create an array Python lists are dynamic - they can grow and shrink on demand Python lists are also heterogenous - Arbitrary data types can be mixed in a single list Python lists are mutable sequences of arbitrary objects

Exercise Write a program which asks a user to enter 10 random numbers. Store the numbers entered in a list. Compute and display the median of the numbers Algorithm sort the numbers in ascending order if the size of the data is odd: median = the middle value else: median = the average of the two middle values return median

Non-sequential Collections This type of collection is called a dictionary In other programming languages, this may be called a HashMap So far we have seen how to look up values in a collection using an index Dictionaries use key-value pairs to store information A collection that allows us to look up information associated with arbitrary keys is called a mapping

Creating a Dictionary >>> passwd = {“guido”:”superprogrammer”, “turing”:”genius”, “bill”:”monopoly”} >>> passwd[“guido”] ‘superprogrammer’ >>>passwd[“bill”] ‘monopoly’ #setting a key-value >>>passwd[“bill”] = “bluescreen” >>>passwd {“guido”:”superprogrammer”, “turing”:”genius”, “bill”:”bluescreen”}

Dictionary Operations OperatorMeaning in Returns true if dictionary contains the specified key, false if it does not.keys()Returns a sequence of keys.values()Returns a sequence of values.items() Returns a sequence of tuples (key, value) representing the key-value pairs.get(, ) If dictionary has key, return its value; otherwise return default del [key] Deletes the specified entry.clear() Deletes all entries for in : Loop over the keys

Dictionary Examples >>> list(passwd.keys()) >>>[‘turing’, ‘bill’, ‘newuser’, ‘guido’] >>> list(passwd.values()) >>>[‘genius’,’bluescreen’,’superprogrammer’] >>>’bill’ in passwd True >>> ‘fred’ in passwd False >>> passwd.clear() >>>passwd {}

Exercise Write a Python program which asks a user to enter a sentence. Using a dictionary, count the number of times each unique word appears in the sentence. Display your result when finished. E.g. The red flowers and the blue flowers bloom in spring The 2 red 1 flowers 2 etc

Exercise Write a Python program using a dictionary which is a simple shopping list that stores items in a list. Ask the user to enter an item, if it is already present in the list, increment the count. The user should enter ‘q’ to quit. E.g. Enter item: >>> bread Enter item: >>> milk Enter item: >>>bread Enter item: >>>q You have in your list: bread 2 milk 1