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

2024-09-30 14:30:37 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我想在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,对吗?我怎么了

下面的问题对我没有帮助,因为append实际上没有任何参数。 It keeps saying: TypeError: append() takes exactly one argument (0 given)

。。。我完全糊涂了。有人能帮我修一下吗?先谢谢你


Tags: the代码incomponentslistprintappendtuple
2条回答

您试图将append作为函数调用,但它是一个方法。因此,不要这样做:

walkingBassList = list.append(walkingBass)

您需要执行以下操作:

walkingBassList.append(walkingBass)

append方法中,self被绑定到walkingBassList,因此它知道要附加到什么。如果没有这些信息,它就无能为力

walkingBassList = list.append(walkingBass)

应该是

walkingBassList.append(walkingBass)

相关问题 更多 >