es102 dictionaries tuples files

Upload: shyam-kumar

Post on 01-Mar-2018

250 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/25/2019 ES102 Dictionaries Tuples Files

    1/24

    Introduction to Computing

    Dictionaries, Tuples, Files

  • 7/25/2019 ES102 Dictionaries Tuples Files

    2/24

    Dictionaries

    >>> eng2sp = dict()

    >>> eng2sp

    {}

    >>> eng2sp['one'] = 'uno>>> eng2sp

    {'one': 'uno'}

    >>> eng2sp = {'one': 'uno', 'two': 'dos', 'three': 'tres'}>>> eng2sp

    {'one': 'uno', 'three': 'tres', 'two': 'dos'}

  • 7/25/2019 ES102 Dictionaries Tuples Files

    3/24

    >>> eng2sp['two']

    'dos

    >>> eng2sp['four']

    KeyError: 'four

    >>> len(eng2sp)

    3

    >>> 'one' in eng2spTrue

    >>> 'uno' in eng2sp

    False

  • 7/25/2019 ES102 Dictionaries Tuples Files

    4/24

    >>> vals = eng2sp.values()

    >>> 'uno' in vals

    True

  • 7/25/2019 ES102 Dictionaries Tuples Files

    5/24

    Tuples

    >>> t = 'a', 'b', 'c', 'd', 'e

    >>> t = ('a', 'b', 'c', 'd', 'e')

    >>> t1 = 'a',

    >>> type(t1)

    >>> t2 = ('a')

    >>> type(t2)

  • 7/25/2019 ES102 Dictionaries Tuples Files

    6/24

    >>> t = tuple()

    >>> t

    ()

    >>> t = tuple('lupins')>>> t

    ('l', 'u', 'p', 'i', 'n', 's')

    >>> t = ('a', 'b', 'c', 'd', 'e')

    >>> t[0]

    'a'

  • 7/25/2019 ES102 Dictionaries Tuples Files

    7/24

    >>> t[1:3]

    ('b', 'c')

    >>> t[0] = 'A'

    TypeError: object doesn't support itemassignment

    >>> t = ('A',) + t[1:]

    >>> t

    ('A', 'b', 'c', 'd', 'e')

  • 7/25/2019 ES102 Dictionaries Tuples Files

    8/24

    Tuple assignment

    >>> temp = a

    >>> a = b

    >>> b = temp

    >>> a, b = b, a

    >>> a, b = 1, 2, 3ValueError: too many values to unpack

  • 7/25/2019 ES102 Dictionaries Tuples Files

    9/24

    >>> addr = '[email protected]'

    >>> uname, domain = addr.split('@')

    >>> uname'monty'

    >>> domain

    'python.org

  • 7/25/2019 ES102 Dictionaries Tuples Files

    10/24

    Dictionaries and tuples

    >>> d = {'a':0, 'b':1, 'c':2}

    >>> t = d.items()

    >>> t

    dict_items([('c', 2), ('a', 0), ('b', 1)])

    >>> for key, value in d.items():

    ... print(key, value)

    ...

    c 2a 0

    b 1

  • 7/25/2019 ES102 Dictionaries Tuples Files

    11/24

    >>> t = [('a', 0), ('c', 2), ('b', 1)]

    >>> d = dict(t)

    >>> d{'a': 0, 'c': 2, 'b': 1}

  • 7/25/2019 ES102 Dictionaries Tuples Files

    12/24

    Dictionaries and lists

    def histogram(s):

    d = dict()

    for c in s:

    if c not in d:

    d[c] = 1

    else:

    d[c] += 1

    return d

  • 7/25/2019 ES102 Dictionaries Tuples Files

    13/24

    >>> h = histogram('brontosaurus')

    >>> h

    {'a': 1, 'b': 1, 'o': 2, 'n': 1, 's': 2, 'r': 2, 'u': 2, 't': 1}

    >>> h = histogram('a')

    >>> h

    {'a': 1}

    >>> h.get('a', 0)

    1

    >>> h.get('b', 0)

    0

  • 7/25/2019 ES102 Dictionaries Tuples Files

    14/24

    Dictionaries and lists

    def histogram(s):

    d = dict()

    for c in s:

    # if c not in d:# d[c] = 1

    # else:

    # d[c] += 1

    d[c]=d.get(c,0)+1

    return d

  • 7/25/2019 ES102 Dictionaries Tuples Files

    15/24

    def print_hist(h):

    for c in h:

    print(c, h[c])

    >>> h = histogram('parrot')

    >>> print_hist(h)

    a 1

    p 1

    r 2

    t 1

    o 1

  • 7/25/2019 ES102 Dictionaries Tuples Files

    16/24

    >>> t = ['a', 'a', 'b']

    >>> hist = histogram(t)

    >>> hist{'a': 2, 'b': 1}

  • 7/25/2019 ES102 Dictionaries Tuples Files

    17/24

    Reverse lookup

    def invert_dict(d):

    inverse = dict()

    for key in d:

    val = d[key]if val not in inverse:

    inverse[val] = [key]

    else:

    inverse[val].append(key)

    return inverse

  • 7/25/2019 ES102 Dictionaries Tuples Files

    18/24

    >>> hist = histogram('parrot')

    >>> hist

    {'a': 1, 'p': 1, 'r': 2, 't': 1, 'o': 1}>>> inverse = invert_dict(hist)

    >>> inverse

    {1: ['a', 'p', 't', 'o'], 2: ['r']}

  • 7/25/2019 ES102 Dictionaries Tuples Files

    19/24

    Application dictionaries

    Word histogramimport string

    def process_file(filename):

    hist = dict()

    fp = open(filename)

    for line in fp:

    process_line(line, hist)return hist

    def process_line(line, hist):

    line = line.replace('-', ' ')

    for word in line.split():

    word = word.strip(string.punctuation + string.whitespace)word = word.lower()

    hist[word] = hist.get(word, 0) + 1

    hist = process_file('emma.txt')

  • 7/25/2019 ES102 Dictionaries Tuples Files

    20/24

    def total_words(hist):

    return sum(hist.values())

    def different_words(hist):

    return len(hist)

    print('Total number of words:', total_words(hist))

    print('Number of different words:',

    different_words(hist))Total number of words: 161080

    Number of different words: 7214

  • 7/25/2019 ES102 Dictionaries Tuples Files

    21/24

    def most_common(hist):

    t = []

    for key, value in hist.items():

    t.append((value, key))

    t.sort(reverse=True)

    return t

    t = most_common(hist)

    print('The most common words are:')

    for freq, word in t[0:10]:

    print(word, '\t', freq)

  • 7/25/2019 ES102 Dictionaries Tuples Files

    22/24

    The most common words are:

    to 5242

    the 5205

    and 4897

    of 4295i 3191

    a 3130

    it 2529

    her 2483was 2400

    she 2364

  • 7/25/2019 ES102 Dictionaries Tuples Files

    23/24

    Files

    >>> fout = open('output.txt', 'w')

    >>> line1 = "This here's the wattle,\n"

    >>> fout.write(line1)

    24

    >>> line2 = "the emblem of our land.\n"

    >>> fout.write(line2)

    24

    >>> fout.close()

  • 7/25/2019 ES102 Dictionaries Tuples Files

    24/24

    Format operator

    >>> x = 52

    >>> fout.write(str(x))

    >>> camels = 42

    >>> '%d' % camels'42

    >>> 'I have spotted %d camels.' % camels

    'I have spotted 42 camels.

    >>> 'In %d years I have spotted %g %s.' % (3, 0.1,'camels')

    'In 3 years I have spotted 0.1 camels.