补交七八次作业

 

 

def read_file():
    f = open('F:\\1.txt')
    readline = f.readlines()
    word = []
    for line in readline:
        line = line.replace(',', '')
        line = line.strip()
        wo = line.split(' ')
        word.extend(wo)
    return word


def clear_account(lists):

    wokey = {}
    wokey = wokey.fromkeys(lists)
    word_1 = list(wokey.keys())
    for i in word_1:
        wokey[i] = lists.count(i)
    return wokey


def sort_1(wokey):
   # del [wokey['']]
    wokey_1 = {}
    wokey_1 = sorted(wokey.items(), key=lambda d: d[1], reverse=True)

    wokey_1 = dict(wokey_1)
    return wokey_1


def main(wokey_1):
    for x, y in wokey_1.items():

        print('the word is "', '{}'.format(x), '"', ' and its amount is "', '{}'.format(y), '"')



main(sort_1(clear_account(read_file())))
输出

C:\Anaconda3\python.exe C:/Users/Administrator/PycharmProjects/pythonProject2/123.py
the word is " a " and its amount is " 4 "
the word is " my " and its amount is " 3 "
the word is " very " and its amount is " 3 "
the word is " to " and its amount is " 2 "
the word is " happy " and its amount is " 2 "
the word is " for " and its amount is " 2 "
the word is " her " and its amount is " 2 "
the word is " farm " and its amount is " 2 "
the word is " there " and its amount is " 2 "
the word is " and " and its amount is " 2 "
the word is " the " and its amount is " 2 "
the word is " Last " and its amount is " 1 "
the word is " summer " and its amount is " 1 "
the word is " American " and its amount is " 1 "
the word is " friend " and its amount is " 1 "
the word is " Maria " and its amount is " 1 "
the word is " come " and its amount is " 1 "
the word is " ChinaI " and its amount is " 1 "
the word is " was " and its amount is " 1 "
the word is " coming. " and its amount is " 1 "
the word is " I " and its amount is " 1 "
the word is " led " and its amount is " 1 "
the word is " visit " and its amount is " 1 "
the word is " uncle's " and its amount is " 1 "
the word is " were " and its amount is " 1 "
the word is " lot " and its amount is " 1 "
the word is " of " and its amount is " 1 "
the word is " cows " and its amount is " 1 "
the word is " sheep " and its amount is " 1 "
the word is " in " and its amount is " 1 "
the word is " also " and its amount is " 1 "
the word is " had " and its amount is " 1 "
the word is " beautiful " and its amount is " 1 "
the word is " pool " and its amount is " 1 "
the word is " we " and its amount is " 1 "
the word is " went " and its amount is " 1 "
the word is " fishing " and its amount is " 1 "
the word is " In " and its amount is " 1 "
the word is " evening " and its amount is " 1 "
the word is " aunt " and its amount is " 1 "
the word is " made " and its amount is " 1 "
the word is " delicious " and its amount is " 1 "
the word is " Chinese " and its amount is " 1 "
the word is " food " and its amount is " 1 "
the word is " us.What " and its amount is " 1 "
the word is " day! " and its amount is " 1 "

Process finished with exit code 0

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2021-12-28 22:11  何泽帆  阅读(37)  评论(0编辑  收藏  举报