Python代码补全:解析音乐符号 (parse_music)
评估大语言模型对Python基础语法(字符串拆分、字典映射、列表推导式)的掌握程度。模型需要根据给定的ASCII音乐符号图例,正确补全函数并输出对应的拍数列表。
Prompt
Complete the following Python function: ```python from typing import List def parse_music(music_string: str) -> List[int]: """ Input to this function is a string representing musical notes in a special ASCII format. Your task is to parse this string and return list of integers corresponding to how many beats does each not last. Here is a legend: 'o' - whole note, lasts four beats 'o|' - half note, lasts two beats '.|' - quater note, lasts one beat >>> parse_music('o o| .| o| o| .| .| .| .| o o') [4, 2, 1, 2, 2, 1, 1, 1, 1, 4, 4] """ ```
Answer guidance
Canonical solution (function body): note_map = {'o': 4, 'o|': 2, '.|': 1} return [note_map[x] for x in music_string.split(' ') if x]