Python需要将包含大写和小写单词的列表更改为所有小写

2024-05-20 19:35:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我的项目很基础。。。我需要一个葛底斯堡地址的文本文件,并计算字数和唯一字数。我几乎一路走到了最后,但它的重复计算的词是一样的,只是一个大写的第一个字母-即,但。我不知道该怎么解决这个问题:(这是我目前所掌握的:

def main():
    getty = open('Gettysburgaddress.txt','r')
    lines = getty.readlines()
    getty.close()

    index = 0
    while index < len(lines):
       lines[index] = lines[index].rstrip('\n')
        index += 1

    words = [word for line in lines for word in line.split()]

    size = len(words)

    print('\nThere are', size,'words in the Gettysburg Address.\n')

    unique = list(set(words))

    size_unique = len(unique)

    print('There are', size_unique,'unique words in the Gettysburg Address.\n')

    unique.sort()

    print('Sorted order of unique words:', unique)

    close = input('')

main()

Tags: inforclosesizeindexlenmainline