Wednesday, February 15, 2017

A Concordance Python

Tags

A concordance is an alphabetical listing of the words in a text String,along with the line numbers on which each word occurs.

Code::

def count(line,dct,linenumber):
    for word in line.split():
        lst=dct.get(word,[])
        lst.append(linenumber)
        dct[word]=lst

line=input("Enter text,quit to end: ")
linenumber=0
dct={}
while line!='quit':
    linenumber+=1
    count(line,dct,linenumber)
    line=input()
for el in sorted(dct.keys()):
    print(el,":",dct[el])

Output::