Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Data Collections CS 127. Lists Lists are ordered sequences of items All programming languages provide a sequence structure similar to a Python list; in."— Presentation transcript:

1 Data Collections CS 127

2 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

3 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

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

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

6 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’]

7 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

8 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

9 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

10 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

11 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

12 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

13 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”}

14 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

15 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 {}

16 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

17 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


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

Similar presentations


Ads by Google