numpy.ndarray 'オブジェクトには属性がありません'追加

Aug 19 2020
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」オブジェクトに属性「append」がないというエラーが発生し続けます。配列test11にユーザーデータを追加できるようにしたい。test11をnumpy配列にすることなく正常に動作します。しかし、私は数のサイズを20に制限できるようにしたいです。何かアイデアはありますか?Plzはそれを簡単にします。

エラーコード:トレースバック(最後の最後の呼び出し):test11.append(data)の10行目AttributeError: 'numpy.ndarray'オブジェクトに属性 'append'がありません

回答

1 ThreePoint14 Aug 19 2020 at 06:50

これは機能する継ぎ目です。もう一度数行変更してマークを付けました。

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を超える数字や文字を使用できなくなり、エラーが表示されます。

JoãoVitorBarbosa Aug 19 2020 at 06:22

Numpy配列には「append」メソッドがありません。必要な形状と長さで作成されるはずです。したがって、あなたの場合、最良の方法は、リストを作成し、値を追加してから、配列を作成することです。

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)
Ehsan Aug 19 2020 at 06:24

行をnp.append次のように置き換えることができます:

np.append(test11,data)

ただし、numpy配列への追加は、リストへの追加よりもコストがかかります。list構造を使用することをお勧めします。append最後に、を使用してリストをnumpy配列に変換します。np.array

リストバージョンは次のとおりです。

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)