下面是完整的代码,该代码将字典中的书面字符转换为乐谱。我想知道是否有人可以向我解释for循环的工作原理。
char2notes = {
' ':("a4 a4 ", "r2 "),
'a':("<c a>2 ", "<e' a'>2 "),
'b':("e2 ", "e'4 <e' g'> "),
'c':("g2 ", "d'4 e' "),
'd':("e2 ", "e'4 a' "),
'e':("<c g>2 ", "a'4 <a' c'> "),
'f':("a2 ", "<g' a'>4 c'' "),
'g':("a2 ", "<g' a'>4 a' "),
'h':("r4 g ", " r4 g' "),
'i':("<c e>2 ", "d'4 g' "),
'j':("a4 a ", "g'4 g' "),
'k':("a2 ", "<g' a'>4 g' "),
'l':("e4 g ", "a'4 a' "),
'm':("c4 e ", "a'4 g' "),
'n':("e4 c ", "a'4 g' "),
'o':("<c a g>2 ", "a'2 "),
'p':("a2 ", "e'4 <e' g'> "),
'q':("a2 ", "a'4 a' "),
'r':("g4 e ", "a'4 a' "),
's':("a2 ", "g'4 a' "),
't':("g2 ", "e'4 c' "),
'u':("<c e g>2 ", "<a' g'>2"),
'v':("e4 e ", "a'4 c' "),
'w':("e4 a ", "a'4 c' "),
'x':("r4 <c d> ", "g' a' "),
'y':("<c g>2 ", "<a' g'>2"),
'z':("<e a>2 ", "g'4 a' "),
'\n':("r1 r1 ", "r1 r1 "),
',':("r2 ", "r2"),
'.':("<c e a>2 ", "<a c' e'>2")
}
txt = "Love one another and you will be happy. It is as simple as that."
upper_staff = ""
lower_staff = ""
for i in txt.lower():
(l,u) = char2notes[i]
upper_staff += u
lower_staff += l
staff = "{\n\\new PianoStaff << \n"
staff += " \\new Staff {" + upper_staff + "}\n"
staff += " \\new Staff { \clef bass " + lower_staff + "}\n"
staff += ">>\n}\n"
title = """\header {
title = "Love One Another"
composer = "Bernd Klein using Python"
tagline = "Copyright: Bernd Klein"
}"""
print (title + staff)
答案 0 :(得分:0)
# Loop over the characters ("i") in txt.lower() - txt converted to lowercase.
for i in txt.lower():
# Assign the values in the char2notes dictionary value corresponding to the key
# (the character within txt.lower()) to variables l and u.
(l,u) = char2notes[i]
# Append u to the upper_staff string
upper_staff += u
# Append l to the lower_staff string
lower_staff += l
答案 1 :(得分:0)
字符串实际上只是字符列表,因此您的for循环遍历字符串中的每个字符,找到与该字符对应的元组(或该字符为大写的小写字母),然后将元组的第一个元素附加到使用+ =运算符输入您的lower_staff字符串,并对第二个元素和upper_staff进行相同操作。