必须创建一个巨大的复杂列表理解来创建 Python 中的 ASCII 值列表字典,不能在列表 comp 之外使用 for 循环
Have to create a huge complicated list comprehension to create dictionary of list of ASCII values in Python, cannot use for loop outside of list comp
The following is my code so far along with the instructions in a docstring.
def get_word_codes(words):
"""Given a list of words, return a dictionary where the words are keys,
and the value for each key is a list of the character codes of the
letters of the word that is its key."""
[print(ascii(words.index(x))) for x in word in words]
Obviously, this code is wrong, but I have worked on it for hours. I know I seem pathetic, but in my defense, my teacher wants me to print the entire code in just one list comprehension. Obviously, she says that for this exercise, she wants us to use PEP8 as a guide. And I know how I need to split up the code into several lines to make it more readable and presentable. That's not a problem. The problem is I am pretty sure that this list comprehension will be ridiculously long. Please, please help. I am very grateful for any help you can give.
Here's how the function should work, if it works correctly:
words = ['yes', 'no']
codes = get_word_codes(words)
codes {'yes': [121, 101, 115], 'no': [110, 111]}
words = ['hello', 'python', 'bye']
codes = get_word_codes(words)
codes {'hello': [104, 101, 108, 108, 111], 'python': [112, 121, 116, 104, 111, 110], 'bye': [98, 121,
101]}
codes = get_word_codes(['Python is fun!'])
codes {'Python is fun!': [80, 121, 116, 104, 111, 110, 32, 105, 115, 32, 102, 117, 110, 33]}
所以列表中的每个单词都变成了一个键,每个单词中的每个字母都是值,但是值应该是字母的ASCII值。
您可以对字典使用列表理解。遍历列表以分隔单词。然后在内部使用列表理解来构建一个带有 ord()
.
的 ascii 值列表
def get_word_codes(words):
"""Given a list of words, return a dictionary where the words are keys,
and the value for each key is a list of the character codes of the
letters of the word that is its key."""
return {word:[ord(x) for x in word] for word in words}
print(get_word_codes(["this", "is", "a", "list", "of", "words"]))
输出:
{'this': [116, 104, 105, 115], 'is': [105, 115], 'a': [97], 'list': [108, 105, 115, 116], 'of': [111, 102], 'words': [119, 111, 114, 100, 115]}
可以通过以下方式实现
def get_word_codes(words: list) -> dict:
return dict([(word, [ord(char) for char in word]) \
for word in words])
if __name__ == "__main__":
print(get_word_codes(["Hello", "World"]))
如果你想理解它:我们使用 pythons 内置函数 ord()
来获取字符串中 char 的 ascii 值,创建了一个以键值对作为其元素的元组,以便将其传递给 dict
创建所需的字典。
The following is my code so far along with the instructions in a docstring.
def get_word_codes(words):
"""Given a list of words, return a dictionary where the words are keys,
and the value for each key is a list of the character codes of the
letters of the word that is its key."""
[print(ascii(words.index(x))) for x in word in words]
Obviously, this code is wrong, but I have worked on it for hours. I know I seem pathetic, but in my defense, my teacher wants me to print the entire code in just one list comprehension. Obviously, she says that for this exercise, she wants us to use PEP8 as a guide. And I know how I need to split up the code into several lines to make it more readable and presentable. That's not a problem. The problem is I am pretty sure that this list comprehension will be ridiculously long. Please, please help. I am very grateful for any help you can give. Here's how the function should work, if it works correctly:
words = ['yes', 'no']
codes = get_word_codes(words)
codes {'yes': [121, 101, 115], 'no': [110, 111]}
words = ['hello', 'python', 'bye']
codes = get_word_codes(words)
codes {'hello': [104, 101, 108, 108, 111], 'python': [112, 121, 116, 104, 111, 110], 'bye': [98, 121,
101]}
codes = get_word_codes(['Python is fun!'])
codes {'Python is fun!': [80, 121, 116, 104, 111, 110, 32, 105, 115, 32, 102, 117, 110, 33]}
所以列表中的每个单词都变成了一个键,每个单词中的每个字母都是值,但是值应该是字母的ASCII值。
您可以对字典使用列表理解。遍历列表以分隔单词。然后在内部使用列表理解来构建一个带有 ord()
.
def get_word_codes(words):
"""Given a list of words, return a dictionary where the words are keys,
and the value for each key is a list of the character codes of the
letters of the word that is its key."""
return {word:[ord(x) for x in word] for word in words}
print(get_word_codes(["this", "is", "a", "list", "of", "words"]))
输出:
{'this': [116, 104, 105, 115], 'is': [105, 115], 'a': [97], 'list': [108, 105, 115, 116], 'of': [111, 102], 'words': [119, 111, 114, 100, 115]}
可以通过以下方式实现
def get_word_codes(words: list) -> dict:
return dict([(word, [ord(char) for char in word]) \
for word in words])
if __name__ == "__main__":
print(get_word_codes(["Hello", "World"]))
如果你想理解它:我们使用 pythons 内置函数 ord()
来获取字符串中 char 的 ascii 值,创建了一个以键值对作为其元素的元组,以便将其传递给 dict
创建所需的字典。