简体   繁体   English

我怎么能 append 一个字符串到列表中的元组?

[英]How I can append a string to the tuples in a list?

I'm creating an application that generates walking bass lines from specified chords.我正在创建一个从指定和弦生成步行低音线的应用程序。 It always begins with the root (1st chord tone), then the permutations of the 1st, 3rd and 5th chord tones follow.它总是从根音(第一和弦音)开始,然后是第一、第三和第五和弦音的排列。

I'd like to append a string (the root) to the tuples (the permutations) in a list in Python, but my code doesn't work.我想 append 一个字符串(根)到 Python 列表中的元组(排列),但我的代码不起作用。 Let me put my code:让我把我的代码:

# 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')]

... This code gives me the following output with an error: ...此代码为我提供了以下 output 错误:

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'

So, I surrounded the tuple with a list:所以,我用一个列表包围了这个元组:

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

Now it gives me TypeError: append() takes exactly one argument (0 given) :现在它给了我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)

... Wait. ... 等待。 (0 given)? (给定0)? One argument walkingBass is already given, right?已经给出了一个参数walkingBass ,对吗? What's wrong with me?我怎么了?

The following question doesn't help me because the append really doesn't have any argument.以下问题对我没有帮助,因为 append 确实没有任何论据。 It keeps saying: TypeError: append() takes exactly one argument (0 given) 它一直在说:TypeError:append() 只接受一个参数(给定 0)

... I'm totally confused. ......我完全糊涂了。 Could anyone help me to fix this?谁能帮我解决这个问题? Thank you in advance.先感谢您。

You're trying to invoke append as a function, but it's a method.您正在尝试将append作为 function 调用,但这是一种方法。 So instead of doing:所以不要这样做:

walkingBassList = list.append(walkingBass)

You need to do:你需要做:

walkingBassList.append(walkingBass)

In the append method, self gets bound to walkingBassList so it knows what to append to.append方法中, self绑定到walkingBassList ,因此它知道 append 到什么。 Without that information, there isn't much it could do.没有这些信息,它就无能为力。

walkingBassList = list.append(walkingBass)

should be应该

walkingBassList.append(walkingBass)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM