Hello,

pls can somebody have a look at my script and tell me why I can't get a value from d1? (print d1). Thank you for help.

here is a script:

def subtract():
    d1=file1(filename)
    dd2=file2(filename)
    res = dict()
    print d1
##    for key in d1:
##        if key not in d2:
##            res[key] = None
##    print res

def file1(filename):
    d1 = dict()
    fp = open(filename)
    for line in fp:
        process_line1(line, d1)
        return d1

def file2(filename):
    d2 = dict()
    fp = open(filename)
    for line in fp:
        process_line(line, d2)
        return d2

def process_line1(line, d1):
    line = line.replace('-', ' ')
    for word in line.split():
        word = word.strip(string.punctuation + string.whitespace)
        word = word.lower()
        d1[word] = d1.get(word, 0) +1

d1 = file1('emma.txt')
d2 = file2('words.txt')

emma.txt consist of these words:

Hossa-už-dlhšie patrí medzi najlepších hrácov v lige, ale to už všetci vedia, preto si nan dávajú velký pozor. Otvára sa tak šanca pre dalších, napríklad aj pre mna. Hossa vie vynikajúco pracovat s pukom, má výborný prehlad v hre," cituje ESPN krídelníka Troya Brouwera, ktorý po dvoch prihrávkach od staršieho z bratov Hossovcov dvakrát rozvlnil siet za gólmanom hostí Leightonom. Hossa Hossa Hossa Hossa Hossa Hossa Hossa Hossa Hossa Hossa Hossa Hossa Hossa Hossa Hossa Hossa Hossa Hossa Hossa

words.txt consist of these words:

Finding the words from the book that are not in the word list from words.txt is a problem you might recognize as set subtraction; that is, we want to find all the words from one set (the words in the book) that are not in another set (the words in the list).

You have not

import string

at least. Also process_line is not defined (there is one function named process_line1 though)
Finally you are not calling any functions only loading two files.

Edited 6 Years Ago by pyTony: n/a

ok, I correct it. But still I am not sure if I did it according to textbook "Think Python: How to Think Like a Computer Scientist, from Allen B. Downey, Version 1.1.22" in chapter 13, 13.6 Dictionary subtraction, there is exercise 6. I don't use the same text but that should not be the issue.

script:

import string
def subtract(filename1,filename2):
    d1=file1(filename1)
    d2=file2(filename2)
    res = dict()
    for key in d1:
        if key not in d2:
            res[key] = None
    print res

def file1(filename):
    d1 = dict()
    fp = open(filename)
    for line in fp:
        process_line1(line, d1)
        return d1

def file2(filename):
    d2 = dict()
    fp = open(filename)
    for line in fp:
        process_line(line, d2)
        return d2

def process_line1(line, d1):
    line = line.replace('-', ' ')
    for word in line.split():
        word = word.strip(string.punctuation + string.whitespace)
        word = word.lower()
        d1[word] = d1.get(word, 0) +1


d1 = file1('emma.txt')
d2 = file2('words.txt')

subtract('emma.txt','words.txt')

Try adding some print statements as below.

def file1(filename):
    d1 = dict()
    fp = open(filename)
    for line in fp:
        print "processing file1 line =", line
        process_line1(line, d1)
        return d1
 
def file2(filename):
    d2 = dict()
    fp = open(filename)
    for line in fp:
        print "processing file2 line =", line
        process_line(line, d2)
        return d2
Comments
good idea !
This question has already been answered. Start a new discussion instead.