You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于区间列表跳过数组元素:Python实现音阶音符提取自动化

实现自动提取音阶的Python方案

直接上可运行的代码,这个函数能根据你给定的音符列表、间隔规则和起始音,自动生成对应的音阶:

notes = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B']
intervals = [2, 1, 2, 2, 1, 2, 2]

def generate_scale(start_note, notes_list, intervals_list):
    # 获取起始音的初始索引
    current_idx = notes_list.index(start_note)
    scale = [start_note]
    # 按间隔依次计算后续音符
    for step in intervals_list:
        current_idx = (current_idx + step) % len(notes_list)
        scale.append(notes_list[current_idx])
    return scale

# 示例:生成C大调音阶
c_major_scale = generate_scale('C', notes, intervals)
print(c_major_scale)
# 输出:['C', 'D', 'E', 'F', 'G', 'A', 'B', 'C']

代码说明

  • generate_scale 函数接收三个参数:起始音、完整音符列表、间隔规则列表
  • 用 notes_list.index(start_note) 定位起始音的位置,确保从正确起点开始
  • 遍历间隔列表时,用 % len(notes_list) 实现音符列表的循环(比如从B往后跳2步会回到C)
  • 最终返回的列表包含起始音和按间隔生成的所有后续音符,完整构成一个8音阶

你可以随意替换起始音生成不同音阶,比如生成G大调音阶:

g_major_scale = generate_scale('G', notes, intervals)
print(g_major_scale)
# 输出:['G', 'A', 'B', 'C', 'D', 'E', 'F#', 'G']

内容的提问来源于stack exchange,提问作者andrwondabeat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 07:50:10