gpt4 book ai didi

Python:字符串比较

转载 作者:行者123 更新时间:2023-12-01 01:54:14 25 4
gpt4 key购买 nike

这里我有一个和弦列表:

chordList = ["N","C:maj","C:min","C#:maj","C#:min","D:maj","D:min","D#:maj","D#:min","E:maj","E:min","F:maj","F:min","F#:maj","F#:min","G:maj","G:min","G#:maj","G#min","A:maj","A:min","A#:maj", "A#:min", "B:maj","B:min"]

例如,有一些字符串,例如“F:maj”,“F:maj/3”,“C:maj/4”,“D:min7”等。我知道我可以在chordList中使用if字符串检查列表中是否有正常的和弦。但是,我想将“F:maj/3”、“C:maj/4”等字符串与列表中的字符串进行比较,无论它们是否具有共同的“x:maj”或“x:min”部分,然后返回 TRUE 或 FALSE 值

最佳答案

我建议将您的 chordList 转换为 set,以获得平均 O(1) 查找时间。

选项 1
/ 上分割:

sample = 'C:maj/7'
sample.split('/')[0] in chordList
# True

选项 2(如果我弹钢琴的日子没记错的话,这将始终有效,尽管如果和弦存在于九度以上,这将失败)
切片

sample = 'C:maj/7'
sample[:-2] in chordList
# True

关于Python:字符串比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50396355/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com