在python中,根据单词的最后两个字符来分隔单词

在python中,根据单词的最后两个字符来分隔单词,python,list,char,Python,List,Char,我有一个分离葡萄牙语单词的程序,以a,as,e,es,o,os结尾。我已经创建了一些列表,我在文件中循环,并根据它们的结尾将文件中的单词分配到这些不同的列表中。与模式不匹配的单词将分配给名为“其他”的列表。 现在,我想根据剩下的两个字符来分隔所有其他单词。我想我也可以这样做:例如,以“em”结尾的单词分配给名为“em”的列表,以“ul”结尾的单词分配给名为“ul”的列表,等等。然而,我最终会得到一个巨大的代码,因为我已经检查过了,还有470个其他结尾!因此,我需要手动创建470个列表。有人知道我

我有一个分离葡萄牙语单词的程序,以a,as,e,es,o,os结尾。我已经创建了一些列表,我在文件中循环,并根据它们的结尾将文件中的单词分配到这些不同的列表中。与模式不匹配的单词将分配给名为“其他”的列表。 现在,我想根据剩下的两个字符来分隔所有其他单词。我想我也可以这样做:例如,以“em”结尾的单词分配给名为“em”的列表,以“ul”结尾的单词分配给名为“ul”的列表,等等。然而,我最终会得到一个巨大的代码,因为我已经检查过了,还有470个其他结尾!因此,我需要手动创建470个列表。有人知道我怎么能自动做到这一点吗?或者其他解决问题的方法?我的代码如下。非常感谢

from nltk.tokenize import sent_tokenize,wordpunct_tokenize
import re
import os
import io
import sys
from pathlib import Path

while True:
    try:
        file_to_open =Path(input("Please, insert your file path: "))
        with open(file_to_open,'r', encoding="utf-8") as f:
            words = f.read().lower()
            break         
    except FileNotFoundError:
        print("\nFile not found. Better try again")
    except IsADirectoryError:
        print("\nIncorrect Directory path.Try again")

other=[]

e=[]
o=[]
a=[]

for y in words:
    if y[-1:] == 'a'or y[-2:]=='as':
        a.append(y)
    elif y[-1:] == 'o' or y[-2:] =='os' :
        o.append(y)
    elif y[-1:] == 'e'or y[-2:]=='es':
        e.append(y)
    else:
        other.append(y)

otherendings=[]

for t in other:
    endings=t[-2:]
    otherendings.append(endings)

print(len(otherendings))
print(set(otherendings)) #470

创建关键字为词尾的词典:

word_dict = {}
for word in words:
    ending = word[-2:]
    try: 
        word_dict[ending].append(word)
    except:
        word_dict[ending] = [word]

在对单词进行迭代之后,您将拥有一个字典,其中键是由两个字母组成的字符串,每个键将包含以这两个字母结尾的单词列表。

非常感谢!真管用!但是如何打印生成的列表的长度,即打印为每个关键字生成的每个列表中的字数