Agenda Announcements Snarf code for class today: - - PowerPoint PPT Presentation

agenda
SMART_READER_LITE
LIVE PREVIEW

Agenda Announcements Snarf code for class today: - - PowerPoint PPT Presentation

Agenda Announcements Snarf code for class today: SortingANDItemgetter Dictionary and type of keys Dictionary and sorting 1/14/2013 CompSci101 Peter Lorensen 1 Dictionary For loop using the following functions: .keys()


slide-1
SLIDE 1

Agenda

  • Announcements
  • Snarf code for class today:

“SortingANDItemgetter”

  • Dictionary and type of keys
  • Dictionary and sorting

1/14/2013 CompSci101 Peter Lorensen 1

slide-2
SLIDE 2

Dictionary

For loop using the following functions:

.keys() .values() .items()

1/14/2013 CompSci101 Peter Lorensen 2

Gives you all the keys in the dictionary Gives you all the values in the dictionary Gives you BOTH the keys and the values in the dictionary as a tuple

slide-3
SLIDE 3

Dictionary

What is printed by this code?

1/14/2013 CompSci101 Peter Lorensen 3

dictPerson= {["Peter", "Lorensen"]: "919-659- 8462",["Owen","Astrachan"]:"919-985-3689" } dictPerson[["Susan", "Rodger"]] = dictPerson.get(["Susan", "Rodger"], "919- 867-3515") dictPerson[["Kate", "Moss"]] = dictPerson.get(["Peter", "Lorensen"], "919- 659-8462") print dictPerson.values() A: [ ["Peter", "Lorensen"],["Owen", "Astrachan"], ["Susan", "Rodger"],["Kate", "Moss"] ] B: [ "919-659-8462", "919-985-3689", "919-867-3515", "919-659-8462"] C: Error D: [ "919-659-8462", "919-985-3689", "919-867-3515" ]

slide-4
SLIDE 4

1/14/2013 CompSci101 Peter Lorensen 4

slide-5
SLIDE 5

Mary Shaw

  • Software engineering and software architecture

– Tools for constructing large software systems – Development is a small piece of total cost, maintenance is larger, depends on well- designed and developed techniques

  • Interested in computer science, programming,

curricula, and canoeing, health-care costs

  • ACM Fellow, Alan Perlis Professor of

Compsci at CMU

  • “Outstanding Research Award” winner from

ACM SIGSOFT

1/14/2013 CompSci101 Peter Lorensen 5

slide-6
SLIDE 6

Dictionary sort()

1/14/2013 CompSci101 Peter Lorensen 6

ages = [18,14,27,23,12,31] print ages.sort() A: [12,14,18,23,27,31] B: None C: Error D: [31,27,23,18,14,12]

slide-7
SLIDE 7

Dictionary sorted()

1/14/2013 CompSci101 Peter Lorensen 7

ages = [18,14,27,23,12,31] print sorted( ages ) A: [12,14,18,23,27,31] B: None C: Error D: [31,27,23,18,14,12]

slide-8
SLIDE 8

Sort( ) vs sorted( )

sort( ) sorted( ) In-place Return None Return list Function in list Built-in function Stable Stable Timsort Timsort

1/14/2013 CompSci101 Peter Lorensen 8

slide-9
SLIDE 9

Sorting need

  • Given a list of tuples, sorted( sequence ) always

uses the first item in the list to sort on:

1/14/2013 CompSci101 Peter Lorensen 9

ages = [("Bob", 37), ("Joe", 21), ("Linda", 27), ("Ant", 37)] print sorted( ages ) >>> [('Ant', 37), ('Bob', 37), ('Joe', 21), ('Linda', 27)]

We often want to sort via the second (or third item) in a list of tuples.

slide-10
SLIDE 10

1/14/2013 CompSci101 Peter Lorensen 10

def topsong(songd): '''songd is dictionary: key is email address corresponding value is list of song titles purchased by person with email address returns: song bought by most people''' mostDict = {} for songList in songd.values(): #songList=['IceBaby','Raise Glass'] for song in songList: #song = 'Ice Ice Baby' mostDict[song] = mostDict.get(song,0) + 1 # Adding new song if not there AND updating existing song at the same time """mostDict now contains this: mostDict = {'song1':3, 'song2':9, 'song3':2, 'song4':4, } halfList = mostDict.items() #[('Ice Ice Baby', 1), ('The Cave', 1), ('Raise Your Glass', 3), ('Landfill', 2), ('Sleepyhead', 1)] """TODO: Go through list of tuples, sort on the second item""" return halfList[0][0] #[('Raise Your Glass', 3), …..]

slide-11
SLIDE 11
  • The itemgetter( ) function simply gets the

element number specified

1/14/2013 CompSci101 Peter Lorensen 11

  • perator.itemgetter(…)

sorted( list , key = func , reverse = True )

  • sorted( ) takes 2 extra arguments.
  • func (key in documentation) is a function that is

applied on every element.

  • perator.itemgetter( itemNumber )
slide-12
SLIDE 12

Operator.itmegetter(…) example

  • You must import the operator to access the

function .itemgetter(..)

  • The funtion is applied for each tuple and you

simply specify which element you want

1/14/2013 CompSci101 Peter Lorensen 12

import operator ages = [("Bob", 37), ("Joe", 21), ("Linda", 27), ("Ant", 37)] print sorted( ages, key = operator.itemgetter(1) ) >>>[('Joe', 21), ('Linda', 27), ('Bob', 37), ('Ant', 37)]

slide-13
SLIDE 13

Try the snarfed project

1/14/2013 CompSci101 Peter Lorensen 13

slide-14
SLIDE 14

Operator.itemgetter(…)

1/14/2013 CompSci101 Peter Lorensen 14

import operator employees = {"Li":("Sale", 1900.00), "Joe":("Sale", 2000.00), "Dale":("Transp",1950.00), "Lyn":("Sale", 2250.00), "Pete":("Transp",2150.00) } lstSort = sorted(employees.values(), key=operator.itemgetter(1), reverse=True) print lstSort[0][0] A: [('Sale', 2250.0), ('Transp', 2150.0), ('Sale', 2000.0), ('Transp', 1950.0), ('Sale', 1900.0)] B: 1900.00 C: [('Sale', 1900.0), ('Transp', 1950.0), ('Sale', 2000.0), ('Transp', 2150.0), ('Sale', 2250.0)] D: 2250.00

slide-15
SLIDE 15

1/14/2013 CompSci101 Peter Lorensen 15