typeerror:list.append()通过迭代附加阵列时,请完全引起一个参数(2给定)错误

发布于 2025-02-13 11:43:13 字数 544 浏览 0 评论 0原文

我希望我的代码找到大写字母的位置并将其添加到数组中。测试后,我得到错误: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

⊕婉儿 2025-02-20 11:43:14

错误是不言自明的。 附加函数仅采用一个参数,但通过了两个参数。

替换以下代码行:

caps.append(capital_quantity,i)

与此:

caps.append(capital_quantity)

The error is self-explanatory. The append function only takes a single parameter but two were passed.

Replace the following line of code:

caps.append(capital_quantity,i)

with this:

caps.append(capital_quantity)
月下伊人醉 2025-02-20 11:43:14

对于列表,您只能一次附加一项。如果您想保留Capital_quantity,并且我在一起可以将它们附加到您的列表中,即

caps.append([capital_quantity,i])

值得注意的话,如果出于任何原因,您想在平坦的结构中添加两个值,而不是嵌套列表使用.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.

caps.append([capital_quantity,i])

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.

三生路 2025-02-20 11:43:14

好像您有list.Appendlist.intert.intert混合在一起。

list.Append将一个参数添加到列表的末尾,而list.insert同时将位置参数和项目插入列表的位置。

此外,您的代码中似乎还有其他错误/修复程序。

  1. 范围(0,25)应为范围(0,26),因为该范围中的最后一项将比最终参数小(数字0-24而不是0-25)
  2. caps = []在每次调用时,将列表caps设置为空列表。我认为这不是你想要的。
  3. 您不需要else:Pass
  4. 您不需要capital_quantity。只需使用list.append。如果您需要计算列表中有多少个首都,则只需做len(caps)

以下是我以最直接的方式实现此问题的方式:

caps=[]
for i,c in enumerate(camel_case):
    if c.isupper():
        caps.append(i)

我们检查每个字符是否c c 在字符串camel_case是大写,如果是大写,我们将其索引i附加到列表中。

Seems like you got list.append and list.insert mixed up.

list.append takes in one argument to add to the end of the list, whereas list.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.

  1. range(0,25) should be range(0,26) since the last item in the range will be one less than the end argument (numbers 0-24 instead of 0-25)
  2. caps=[] sets the list caps to an empty list every time it's called. I don't think that's what you want.
  3. You don't need the else:pass
  4. You don't need the capital_quantity. Just use list.append. If you need to count how many capitals are in the list, just do len(caps)

Here's how I'd implement this problem the most straightforward way:

caps=[]
for i,c in enumerate(camel_case):
    if c.isupper():
        caps.append(i)

We check if each character c in the string camel_case is uppercase, and if it is, we append its index i to the list.

倒带 2025-02-20 11:43:14

您可以在列表中保存两个或多个数据,其中具有类似的卷发括号。

caps.append({capital_quantity,i})

You can save two or more data in a list with curly braces like this.

caps.append({capital_quantity,i})
看海 2025-02-20 11:43:14

使用list.extend方法

caps.extend([[Capital_quantity,i])

caps.extend((capital_quantity,i))

Use list.extend method

caps.extend([capital_quantity,i]) or

caps.extend((capital_quantity,i))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文