1 回答
TA贡献1877条经验 获得超1个赞
该类没有任何作用,它只是混淆了你正在做的事情。要将代码音译为dict直接使用 a,您只需要:
result = {}
for TYPE in lettered_word_types:
result[TYPE + 's'] = [letter + TYPE for letter in A_to_Z]
result = {key: [[element + "1", element + "2", element + "3"] for element in value] for key, value in result.items()}
所以,只是为了证明这确实是等价的:
In [1]: import string
...: A_to_Z = list(string.ascii_uppercase) # list of letters a-z
...: lettered_word_types = ["verb", "adjective", "name", "noun"]
...:
...: class Lexicon:
...: def __init__(self, testing = True):
...: if testing == True:
...: for TYPE in lettered_word_types:
...: setattr(self, TYPE + "s", [letter + TYPE for letter in A_to_Z])
...: self.__dict__ = {key: [[element + "1", element + "2", element + "3"] for element in value] for key, value in self.__dict__.items()}
...:
...:
...: data = Lexicon().__dict__
In [2]: result = {}
In [3]: for TYPE in lettered_word_types:
...: result[TYPE + 's'] = [letter + TYPE for letter in A_to_Z]
...:
In [4]: result = {key: [[element + "1", element + "2", element + "3"] for element in value] for key, value in result.items()}
In [5]: result == data
Out[5]: True
添加回答
举报