Transforming a stream of records

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Transforming a stream of records

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A stream of records holds prices as text, such as "12.50". Which step converts each price so it can be used in calculations?
A Convert each price with float(row[2])
B Leave the price as text and add 1 to it
2. Which line transforms every record in the stream with map()?
A clean = map(tidy, records)
B clean = filter(tidy, records)
3. A student name is stored as " aVA nguyen ". Which expression trims the spaces and fixes the case?
A name.split().count()
B name.strip().title()
4. After transforming a stream with map(), how many records come out compared with how many went in?
A Fewer, because map() removes some records
B The same number, each one changed
5. Each record has price and qty. Which is a derived field transform?
A total = price * qty
B total = price + "qty"

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1int(row[2]) ARemoves extra spaces from both ends of the text and capitalises each word
2name.strip().title() BThe brackets are missing, so the method is not called and the text is never changed. It should be name.upper().
3Transform every record in a stream by converting its price field from text to a float Ctotal = price * qty
4Add a derived field to each record by calculating a new value from existing fields D[float(r[1]) for r in rows]
5price = row[2] total = price * 2 EConverts a text field from a CSV line into a whole number so it can be used in calculations
6name.upper Frow[2] is still a string from the CSV file, so it must be converted with int() or float() first. Otherwise the multiplication repeats the text instead of calculating a number.

Definitions

map

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
prices = list(___(float, raw_prices))
2.
ages = list(map(___, ages_text))
3.
names = list(map(lambda s: s._____(), raw))
4.
gross = list(map(lambda p: p _ 1.1, nums))
5.
shout = list(map(str._____, houses))

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A rows = ["5", "12", "8"]
B nums = list(map(int(), rows))
C print(nums)
Question 2
A names = [" ava ", "LIAM", "mia "
B clean = []
C for n in names:
D clean.append(n.strip().title())
Question 3
A prices = ["4.50", "12", "3.25"]
B nums = [float(p) for p in price]
C print(nums)
Question 4
A rows = [("Pen", 2, 1.5)]
B out = []
C for name, qty, price in rows:
D out.append((name, qty + price))
E print(out)
Question 5
A def clean(text):
B text = text.strip()
C return text.upper
D print(clean(" ava "))

Answer Key

Transforming a stream of records
Section A: Quick Questions
Q1
A — Convert each price with float(row[2])
Q2
A — clean = map(tidy, records)
Q3
B — name.strip().title()
Q4
B — The same number, each one changed
Q5
A — total = price * qty
Section B: Match the Pairs
Q1
int(row[2]) → Converts a text field from a CSV line into a whole number so it can be used in calculations
Q2
name.strip().title() → Removes extra spaces from both ends of the text and capitalises each word
Q3
Transform every record in a stream by converting its price field from text to a float → [float(r[1]) for r in rows]
Q4
Add a derived field to each record by calculating a new value from existing fields → total = price * qty
Q5
price = row[2] total = price * 2 → row[2] is still a string from the CSV file, so it must be converted with int() or float() first. Otherwise the multiplication repeats the text instead of calculating a number.
Q6
name.upper → The brackets are missing, so the method is not called and the text is never changed. It should be name.upper().
Section C: Definitions
Q1
map — map() applies a function to every record in a stream and gives back changed records, such as converting a type, trimming and changing the case of text, or computing a derived field.
Section D: Fill in the Blanks
Q1
prices = list(map(float, raw_prices))
Q2
ages = list(map(int, ages_text))
Q3
names = list(map(lambda s: s.strip(), raw))
Q4
gross = list(map(lambda p: p * 1.1, nums))
Q5
shout = list(map(str.upper, houses))
Section E: Spot the Error
Q1
B — map() needs the function itself, not a call to it. int() with no argument raises an error. It should be map(int, rows), which converts each string to an integer.
Q2
A — The list is missing its closing square bracket. The line should end with ] so the list of raw names is complete before each one is trimmed and title-cased.
Q3
B — The variable is called prices, not price, so the comprehension fails with a NameError. It should loop over prices to convert each string to a float.
Q4
D — The derived field is a line total, which is quantity times price. Adding them gives the wrong value. It should be qty * price.
Q5
C — upper is a method and must be called with brackets. Without them the function returns the method object instead of the changed text. It should be text.upper().