会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132647个问题
Python 全系列/第四阶段:函数式编程和核心特性/正则表达式 556楼
Python 全系列/第四阶段:函数式编程和核心特性/函数式编程 557楼
Python 全系列/第四阶段:函数式编程和核心特性/函数式编程 558楼
Python 全系列/第四阶段:函数式编程和核心特性/生成器和装饰器 559楼
Python 全系列/第四阶段:函数式编程和核心特性/生成器和装饰器 560楼
Python 全系列/第四阶段:函数式编程和核心特性/生成器和装饰器 561楼

def dictionary_reader(filename):

    """
    :param filename: The type of the filename is string,
                    which means users need to input a string
                    type's filename.
    :return: a list which contain each word in the file
    """
    words_no_punctuation = []
    words_less_than_9 = []

    try:
        # open the file and execute the read operation
        f = open(filename, 'r')
        # append the each word speratly into a list
        words = f.readlines()
        for word in words:
            words_no_punctuation.append(word.strip())
        for word in words_no_punctuation:
            if len(word) <= 9:
                words_less_than_9.append(word)
        # print(len(words_less_than_9))


        # This is the test part of the dictionary_reader
        # Test passd, if length of list equal to 235887
        x = ''
        for word in words_less_than_9:
            if len(word) > 9:
                x = False
                break
            else:
                x = True
                continue

        if x == False:
            print("Not all of the len of word in this list, which is less than 9 characters.")
        else:
            print("-----Test passed! All words in this list are less than 9 characters-----")




        if len(words) == 235887:
            print("-----Test passed! There are 235887 words in the list!-----")
        else:
            print("The len of words is not equal to the len of word in the words.txt.")

        if isinstance(words_less_than_9, list):
            print("-----Test pssed! The function dictionary_reader will return the all words which len are less than 9 characters in a list!-----")
        else:
            print("The return stuff is not the list type.")
        return words_less_than_9
    except BaseException as e:
        # print the error information when programme meet error
        # it is easy to help users matain the programme
        print(e)
    finally:
        # make sure the file stream will be closed in any suitation of programme
        f.close()
        
        
def word_lookup():
    # read file at first
    # words is a list, and each elements in the words list is a word.
    words = dictionary_reader('words.txt')
    sorted_dictionary = []
    longest_words = []
    possibility_words = []
    for word in words:
        """  
            every single word in the words list will be sorted by alphabetical.
        """
        word = word.strip()
        # ensure every single word in the list is lowercase
        sorted_dictionary.append("".join(sorted(word)).lower())
    print(sorted_dictionary)
words.txt
WechatIMG945.png

老师您好,这是我写的的两个函数。在word_lookup里面我尝试把文件里面的每一个单子都变成字母排序,但是控制台输出的第一个单词并不是按字母排序去排序的,请问一下这是为什么?

输出在附件里面传上去了,不知道为什么不能够上传图片,抱歉。

谢谢老师

Python 全系列/第四阶段:函数式编程和核心特性/函数式编程 562楼
Python 全系列/第四阶段:函数式编程和核心特性/函数式编程 568楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637