python - 使用 "list.extend"时“int”对象不可迭代

标签 python list

<分区>

您好,有人可以帮助我处理这段代码吗?我在第 28 行(A.extend(n))收到错误消息:“‘int’对象不可迭代”,因为我是 python 的新手,所以我无法找出一个解决方案感谢任何帮助

编辑:我之前尝试过 append 并且之前遇到内存错误,想知道 extend() 是否是添加元素的正确方法,但似乎我犯了一个错误并以无限循环告终 感谢您的建议,它真的帮助了我

print("Ax^2+Bx+C")
a = int(input("a"))
b = int(input("b"))
c = int(input("c"))
i, j, k, l = 0, 0, 0, 0
A = []
C = []
B = []
ano = []  
bno = []  
no = 0
noc = 0  
n = 2
a2 = a
c2 = c

if (a != 1) or (b != 1):
while i != 1:
    while a2 % n == 0 and c2 % n == 0:
        if a2 % n == 0:
            a2 /= n
            # A.extend(n)
            no += 1
        if c2 % n == 0:
            c2 /= n
            # A.extend(n)
            no += 1
    A.extend(n)
    ano.extend(no)
    no = 0
    n += 1
    if a2 == 1:
        A.extend(1)
        A.extend(1)  
        i = 1

最佳答案

您要查找的是append 而不是extend

>>> a = []

list.extend 不适用于单个项目

>>> a.extend(1)
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    a.extend(1)
TypeError: 'int' object is not iterable

list.append 将项目添加到 list

的末尾
>>> a.append(1)
>>> a
[1]

list.extend的作用是在当前列表的末尾添加另一个list,例如

>>> a.extend([2,3,4])
>>> a
[1, 2, 3, 4]

关于python - 使用 "list.extend"时“int”对象不可迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50296860/

相关文章:

python - Allure -pytest : AttributeError: 'module' object has no attribute 'attachment_type'

python - PyMC3 高斯混合模型

Java List和Collection排序查询

java - 从 EditText 将输入的项目添加到列表(使用 SimpleAdapter)

java - 列表<E>实现

Python 元组列表

python - 双曲 Curve_Fit 的预测区间 - SciPy

python - 在 numpy 矩阵中存储字符串

python - 从 fixture 内部跳过测试

F#中的列表理解