typeerror:list.append()通过迭代附加阵列时,请完全引起一个参数(2给定)错误
我希望我的代码找到大写字母的位置并将其添加到数组中。测试后,我得到错误:typeError:list.append()完全参数(2给定) 同样,当使用带有单数大写字母的输入测试时,它可以正常工作,但是当它们是多个大写字母时,该数组仅包含最后一个位置。
for i in range(0,length):
letter = camel_case[i]
for k in range(0,25):
check = capitals[k]
if check == letter:
position = i
print(f"{position}")
global caps
caps = []
caps.append(capital_quantity,i)
capital_quantity = capital_quantity + 1
else:
pass
i want my code to find the position of capital letters and add them to an array. After testing, i get the error: TypeError: list.append() takes exactly one argument (2 given)
also when testing with a input with a singular capital letter it works fine, however when they are multiple capital letters, the array will only contain the last position.
for i in range(0,length):
letter = camel_case[i]
for k in range(0,25):
check = capitals[k]
if check == letter:
position = i
print(f"{position}")
global caps
caps = []
caps.append(capital_quantity,i)
capital_quantity = capital_quantity + 1
else:
pass
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
错误是不言自明的。
附加
函数仅采用一个参数,但通过了两个参数。替换以下代码行:
与此:
The error is self-explanatory. The
append
function only takes a single parameter but two were passed.Replace the following line of code:
with this:
对于列表,您只能一次附加一项。如果您想保留Capital_quantity,并且我在一起可以将它们附加到您的列表中,即
值得注意的话,如果出于任何原因,您想在平坦的结构中添加两个值,而不是嵌套列表使用.extend()方法。 this 是理解行为的好教程。
For lists, you can only append one item at a time. If you would like to keep capital_quantity and i together you could append them to your list as a secondary list i.e.
Its worth noting, if for whatever reason you want to add both values to your list in a flat structure rather than a nested list you could use the.extend() method. This is a good tutorial to understand the behavior.
好像您有
list.Append
和list.intert.intert
混合在一起。list.Append
将一个参数添加到列表的末尾,而list.insert
同时将位置参数和项目插入列表的位置。此外,您的代码中似乎还有其他错误/修复程序。
范围(0,25)
应为范围(0,26)
,因为该范围中的最后一项将比最终参数小(数字0-24而不是0-25)caps = []
在每次调用时,将列表caps
设置为空列表。我认为这不是你想要的。else:Pass
capital_quantity
。只需使用list.append
。如果您需要计算列表中有多少个首都,则只需做len(caps)
以下是我以最直接的方式实现此问题的方式:
我们检查每个字符是否
c
c 在字符串camel_case
是大写,如果是大写,我们将其索引i
附加到列表中。Seems like you got
list.append
andlist.insert
mixed up.list.append
takes in one argument to add to the end of the list, whereaslist.insert
takes both a positional argument and the item to insert in that position of the list.Additionally, there appears to be other bugs/fixes in your code.
range(0,25)
should berange(0,26)
since the last item in the range will be one less than the end argument (numbers 0-24 instead of 0-25)caps=[]
sets the listcaps
to an empty list every time it's called. I don't think that's what you want.else:pass
capital_quantity
. Just uselist.append
. If you need to count how many capitals are in the list, just dolen(caps)
Here's how I'd implement this problem the most straightforward way:
We check if each character
c
in the stringcamel_case
is uppercase, and if it is, we append its indexi
to the list.您可以在列表中保存两个或多个数据,其中具有类似的卷发括号。
You can save two or more data in a list with curly braces like this.
使用list.extend方法
caps.extend([[Capital_quantity,i])
或caps.extend((capital_quantity,i))
Use list.extend method
caps.extend([capital_quantity,i])
orcaps.extend((capital_quantity,i))