Coder Social home page Coder Social logo

exercise06's Introduction

Exercise 06: Files and dictionaries

##Dictionaries

###What is a dictionary?

Imagine a list as being a numbered sequence of data.

fish = ["shark", "ray", "halibut", "tuna", "squid"]

The first element is shark, and you access it by its index number:

print fish[0]
=> shark

To access the nth element, its index number is n-1. The fifth element is accessed thusly:

print fish[4]
=> squid

What if, instead of a numbered index, we could use a string as an index?

print fish["deep ocean"]
=> anglerfish
print fish["shallow river"]
=> candiru

We would then need another way of specifying a list where each element is named.

fish = {"deep ocean": "anglerfish", "amazon river": "candiru", "lake": "bass", "shallow river": "trout"}

This is called a dictionary in python. It's also called a hashtable, or a hashmap, or very non-specifically, a map. A dictionary is a collection of 'key-value pairs'. The key 'deep ocean' maps to the value 'anglerfish'.

###Why use a dictionary? Imagine you were writing a program to keep track of user scores in a game. If you only had arrays, you might do something like this:

names  = ["Bob", "Joe", "Jack", "Jane"]
scores = [   10,     3,      6,     15]

To find Joe's score, first you'd have to find which position "Joe" is in, then use that position to look up his score.

index_joe = names.index("Joe")
print "Joe's score is %d" % (scores[index_joe])
=> Joe's score is 3

This is unwieldy and complicated. With dictionaries, we could instead do the following:

scores = {"Bob": 10, "Joe": 3, "Jack": 6, "Jane": 15}
print "Joe's score is %d" % (scores['Joe'])
=> Joe's score is 3

###Some useful methods: ####get() Dictionaries have a method called 'get' which allows you to have a default value in case a key does not exist beforehand.

scores = {"Bob": 10, "Joe": 3, "Jack": 6, "Jane": 15}

print scores.get("Bob", 0) # The second argument is the fallback number if the key doesn't exist
=> 10

print scores.get("Billy", 0) # Billy doesn't exist in the dictionary, so return the fallback instead
=> 0

####iteritems() Dictionaries can also be iterated entry-by-entry, using the method iteritems().

For example:

my_dict = {'a': 1, 'b': 2, 'c': 3}
for key, value in my_dict.iteritems():
    print "Key == %r, value == %r" % (key, value)

Prints:

Key == 'a', value == 1
Key == 'b', value == 2
Key == 'c', value == 3

This introduces two loop variables, 'key' and 'value', that will store the key and value elements of each dictionary entry in turn.

###Dictionary Exercises (do these first):

##File Parsing

###Read lines from a file In exercise 5 you read a file and processed one character at a time. Files can also be iterated line-by-line, using a for loop on the file directly.

For example:

twain = open('twain.txt')
for line in twain:
    # Do something

The loop variable line will store each line of the file in turn.

File manipulation Exercises (and sometimes strings):

Description

In this directory, you will find a text file, scores.txt, containing a series of local restaurant ratings. Each line looks like this:

Restaurant Name:Rating

Your job is to write a program named 'sorted_data.py' reads the file, then spits out the ratings in alphabetical order by restaurant

Sample output:

Meringue:Exercise07 chriszf$ python sorted_data.py
Restaurant 'Andalu' is rated at 3.
Restaurant "Arinell's" is rated at 4.
Restaurant 'Bay Blend Coffee and Tea' is rated at 3.
Restaurant 'Casa Thai' is rated at 2.
Restaurant 'Charanga' is rated at 3.
Restaurant 'El Toro' is rated at 5.
Restaurant 'Giordano Bros' is rated at 2.
Restaurant "Irma's Pampanga" is rated at 5.
Restaurant 'Little Baobab' is rated at 1.
Restaurant 'Pancho Villa' is rated at 3.
Restaurant 'Taqueria Cancun' is rated at 2.
Restaurant 'Urbun Burger' is rated at 1.

exercise06's People

Contributors

hackbrightsalesforcegithub avatar katereese avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.