首页 > 解决方案 > 打印时如何排除逗号、方括号和大括号?(Python)

问题描述

我希望下面代码的打印语句排除逗号、方括号和大括号。像这样:

三音符三阶进阶:D♯ C F♭ᵒ C♯</p>

起始位置:632根位置

# Three-Note Triad Generator
import random
roots = ("A", "A♯&quot;, "B", "B♯&quot;, "C", "C♯&quot;, "D", "D♯&quot;, "E", "E♯&quot;, "F", "F♯&quot;, "G", "G♯&quot;, "A♭&quot;, "B♭&quot;, "C♭&quot;, "D♭&quot;, "E♭&quot;, "F♭&quot;, "G♭&quot;)
triad_types = ('', '⁻', '⁺', 'ᵒ')
three_string_groups = ('654', '653', '652', '651', '643', '642', '641', '632', '631', '621', '543', '542', '541', '532','531', '521', '432', '431', '421', '321')
inversion = ("Root Position", "1st Inversion", "Second Inversion")

print("\n\n\nTHREE-NOTE TRIADS PROGRESSION:")
print((random.sample(roots, 1)) + (random.sample(triad_types, 1)))
print((random.sample(roots, 1)) + (random.sample(triad_types, 1)))
print((random.sample(roots, 1)) + (random.sample(triad_types, 1)))
print((random.sample(roots, 1)) + (random.sample(triad_types, 1)))
print("\nStarting Position:")
print((random.sample(three_string_groups, 1)) + (random.sample(inversion, 1)))

标签: python

解决方案


您可以使用 unpacking with*从列表中获取字符串。

例如:

print(*(random.sample(roots, 1) + random.sample(triad_types, 1)))

哪个打印:

B♭

推荐阅读