如何将字符串附加到列表中的元组?

问题描述 投票:0回答:2

我正在创建一个应用程序,该应用程序从指定的和弦生成低音提琴行。它总是以根音(第一和弦音)开始,然后是第一,第三和第五和弦音的排列。

我想在Python列表中将字符串(根)附加到元组(排列),但是我的代码不起作用。让我输入我的代码:

# pip install pychord

from pychord import Chord
from itertools import permutations 

def returnPermutations(arr, r): 
    return list(permutations(arr, r))

c7 = Chord("C7")
print("c7.components()", c7.components())
print("Get the Triad: c7.components()[0:3]: ", c7.components()[0:3])

chordTonePermutations = returnPermutations(c7.components()[0:3], 3)
print("chordTonePermutations: ", chordTonePermutations)
print("chordTonePermutations[0]: ", chordTonePermutations[0])

# walkingBass = tuple((c7.components()[0],)) + chordTonePermutations[0]
walkingBass = list(tuple((c7.components()[0],)) + chordTonePermutations[0])

walkingBassList = []
# walkingBassList = list.append(walkingBass)

for chordTones in chordTonePermutations:
  print("chordTones", chordTones)
  walkingBass = tuple((c7.components()[0],)) + chordTones
  # walkingBass = list(tuple((c7.components()[0],)) + chordTones)
  walkingBassList = list.append(walkingBass)

print("walkingBass", walkingBass)

print("walkingBassList", walkingBassList)
# What I expect is a list of tuples which always begins with the root 'C', 
# plus the permutations of the triad (the first 3 chord tones):
# walkingBassList [
# ('C', 'C', 'E', 'G'), 
# ('C', 'C', 'G', 'E'), 
# ('C', 'E', 'C', 'G'), 
# ('C', 'E', 'G', 'C'), 
# ('C', 'G', 'C', 'E'), 
# ('C', 'G', 'E', 'C')]

...此代码为我提供以下输出,但有错误:

c7.components() ['C', 'E', 'G', 'Bb']
Get the Triad: c7.components()[0:3]:  ['C', 'E', 'G']
chordTonePermutations:  [('C', 'E', 'G'), ('C', 'G', 'E'), ('E', 'C', 'G'), ('E', 'G', 'C'), ('G', 'C', 'E'), ('G', 'E', 'C')]
chordTonePermutations[0]:  ('C', 'E', 'G')
chordTones ('C', 'E', 'G')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-84-3a01c9d6da74> in <module>()
     24   walkingBass = tuple((c7.components()[0],)) + chordTones
     25   # walkingBass = list(tuple((c7.components()[0],)) + chordTones)
---> 26   walkingBassList = list.append(walkingBass)
     27 
     28 print("walkingBass", walkingBass)

TypeError: descriptor 'append' requires a 'list' object but received a 'tuple'

所以,我用一个列表包围了元组:

  # walkingBass = tuple((c7.components()[0],)) + chordTones
  walkingBass = list(tuple((c7.components()[0],)) + chordTones)

现在它给了我TypeError: append() takes exactly one argument (0 given)

TypeError                                 Traceback (most recent call last)
<ipython-input-86-e2ecc32a34c8> in <module>()
     24   # walkingBass = tuple((c7.components()[0],)) + chordTones
     25   walkingBass = list(tuple((c7.components()[0],)) + chordTones)
---> 26   walkingBassList = list.append(walkingBass)
     27 
     28 print("walkingBass", walkingBass)

TypeError: append() takes exactly one argument (0 given)

...等等。 (0分)?已经给出一个参数walkingBass,对吗?我怎么了?

以下问题无济于事,因为追加实际上没有任何参数。It keeps saying: TypeError: append() takes exactly one argument (0 given)

...我完全感到困惑。谁能帮我解决这个问题?预先谢谢你。

python list arguments append tuples
2个回答
3
投票

您正在尝试将append用作函数,但这是一种方法。所以不要这样做:

walkingBassList = list.append(walkingBass)

您需要做:

walkingBassList.append(walkingBass)

append方法中,self被绑定到walkingBassList,因此它知道要追加的内容。没有这些信息,它将无能为力。


2
投票
walkingBassList = list.append(walkingBass)

应该是

walkingBassList.append(walkingBass)

0
投票

tuples是不可变的,因此您不能直接添加到它。但是,您可以执行以下操作:

tuple((c7.components()[0],)) + (chordTones,)
© www.soinside.com 2019 - 2024. All rights reserved.