【问题标题】:numpy.ndarray' object has no attribute 'appendnumpy.ndarray' 对象没有属性 'append
【发布时间】:2020-12-08 04:22:46
【问题描述】:
import numpy as np

Student1= [1,2]
test11= np.array([])
np.clip(0,1,20)
NOF=int(2) 
print("Enter test score, students name are (1,2,3, etc): ")
for i in range(NOF):
    data=int(input())
    test11.append(data)
total=0
for value in test11:
    total=total+value
print("The sum of all", total)

列表版本

import numpy as np

Student1= [1,2]
test11= []
NOF=int(2) 
print("Enter test score, students name are (1,2,3, etc): ")
for i in range(NOF):
    data=int(input())
    test11.append(data)
total=0
for value in test11:
    total=total+value
print("The sum of all", total)

这会一直报错“numpy.ndarray”对象没有“附加”属性。我希望能够将用户数据添加到数组 test11。无需使 test11 成为 numpy 数组即可正常工作。但我希望能够将数字的大小限制为 20。有什么想法吗?请让它变得简单。

错误代码: 回溯(最近一次通话最后): 第 10 行,在 test11.append(数据) AttributeError: 'numpy.ndarray' 对象没有属性 'append'

【问题讨论】:

  • numpy.ndarray 对象确实没有 append 方法。为什么不使用list
  • 请提供完整的错误输出,以及minimal reproducible example
  • np.clip 行对您没有任何作用。重新阅读它的文档。

标签: python numpy


【解决方案1】:

这看起来很有效。我又改了几行,做了标记。

import numpy as np

Student1= [1,2]
test11= np.array([0])
NOF=int(2) 
print("Enter test score, students name are (1,2,3, etc): ")
data = None
while data is None:  # Remove this if you want the program to end if an error occurres.
    for i in range(NOF):
        try:  # Be sure the input is a int.
            data=np.array([int(input())])
            if data > 20:  # Be sure the input is <= 20.
                data = None  # If greater then 20. Turn data into None
                test11 = np.array([0])  # Empty the array.
                print("Error")
                break  # Then break out of the loop
            test11 = np.append(data, test11)
        except ValueError:
            data = None  # If it is not a int, data will trun into None
            test11 = np.array([0])  # Empty the array.
            print("Error")
            break

if data is not None:  # If data is not None then find the sum.
    total=0
    for value in test11:
        total = test11.sum()  # Use the sum fuction, though  total=total+value  will also work.
    print("The sum of all", total)

列出版本。

# import numpy as np

Student1= [1,2]
test11= []
NOF=int(2) 
print("Enter test score, students name are (1,2,3, etc): ")
data = None  # Assign ahead of time.
while data is None:  # Remove this if you want the program to end if an 
error occurres.
    for i in range(NOF):
        try:  # Be sure the input is a int.
            data=int(input())
            if data > 20:  # Be sure the input is <= 20.
                data = None  # If greater then 20. Turn data into None
                test11.clear()  # Clear the list if an error occurres.
                print("Error")
                break  # Then break out of the loop
            test11.append(data)
        except ValueError:
            data = None  # If it is not a int, data will trun into None
            test11.clear()  # Clear the list if an error occurres.
            print("Error")
            break

if data is not None:  # If data is not None then find the sum.
    total=0
    for value in test11:
        total=total+value
    print("The sum of all", total)

这是尽我所能做到的紧凑,无需更改整个内容,并保持与您开始时的内容相似。

现在用户不能使用超过20的数字,或者任何字母,否则会出现错误。

【讨论】:

  • 对不起,我没说对。剪辑功能没用,因为如果用户键入大于 20 的数字,我希望代码出错
  • 如果你能帮上忙的话。如果不是,我非常感谢你帮助我实施 np。
  • 我们不鼓励使用np.append。列表追加更快更容易使用。
  • 在 hpauli 的评论之上,我想补充一点:您不需要创建一个数组来将一个数字附加到数组中。而你的第二个 for 循环是无关紧要的!循环在没有任何目的的情况下重复做同样的事情。此外,您无需在要分配新值的循环之前设置total=0
  • 再次感谢您的帮助
【解决方案2】:

Numpy 数组没有“追加”方法,它们应该以您需要的形状和长度创建。所以在你的情况下,最好的方法是创建一个列表,附加值,然后创建数组:

import numpy as np 
Student1= [1,2] 
test11= []
np.clip(0,1,20) 
NOF=int(2) 
print("Enter test score, students name are (1,2,3, etc): ") 
for i in range(NOF): 
    data=int(input()) 
    test11.append(data)

test11 = np.array(test11)

【讨论】:

    【解决方案3】:

    你可以用np.append替换你的行:

    np.append(test11,data)
    

    但是追加到 numpy 数组比追加到列表更昂贵。我建议将list 结构与append 一起使用,最后使用np.array 将您的列表转换为numpy 数组

    这是一个列表版本:

    import numpy as np
    
    Student1= [1,2]
    test11= []
    np.clip(0,1,20)
    NOF=int(2) 
    print("Enter test score, students name are (1,2,3, etc): ")
    for i in range(NOF):
        data=int(input())
        test11.append(data)
    test11 = np.array(test11)
    total = test11.sum()
    print("The sum of all", total)
    

    【讨论】:

    • 当我用 'np.append(test11,data)' 替换它时,它不再累加了:(
    • 我先试试list方法
    • @GlitterPony 您需要将totol=0; for value in test11: total=total+value 替换为total = test11.sum(),但我再次建议使用列表。另外,请查看stackoverflow.com/help/someone-answers,了解如何接受关于 SO 的答案并欢迎来到 SO。谢谢。
    • 列出了但我在哪里输入 np.array 到
    • @GlitterPony 添加列表版本发布
    猜你喜欢
    • 2017-08-14
    • 2019-11-09
    • 1970-01-01
    • 1970-01-01
    • 2020-09-22
    • 2020-02-21
    • 2019-12-10
    • 2017-07-10
    • 2020-05-10
    相关资源
    最近更新 更多