Chapter 5. Hashes and Databases: Putting data in its place

image with no caption

Arrays aren’t the only show in town when it comes to data.

Programming languages come with other data-arranging goodies too, and our chosen tool, Python, is no exception. In this chapter, you’ll associate values with names using a data structure commonly called the hash (better known as dictionary to Python-folk). And when it comes to working with stored data, you’ll read data from an external database system as well as from regular text-based files. All the world’s awash with data, so turn the page and start applying your ever-expanding programming skills to some cool data-processing tasks.

Who won the surfing contest?

In the previous chapter, you worked out the top three scores, but they’re not much use without the names of the surfers that achieved those scores. There will no be surfing for you until you’ve finished the program.

Here’s the code so far:

scores = []
result_f = open("results.txt")
for line in result_f:
    (name, score) = line.split()
    scores.append(float(score))
result_f.close()
scores.sort()
scores.reverse()
print("The top scores were:")
print(scores[0])
print(scores[1])
print(scores[2])
image with no caption
image with no caption

Get Head First Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.