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는 간단합니다.

오류 코드 : 역 추적 (최근 호출 마지막 호출) : 10 행, test11.append (data) 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 배열에는 '추가'방법이 없으며 필요한 모양과 길이로 생성되어야합니다. 따라서 귀하의 경우 가장 좋은 방법은 목록을 만들고 값을 추가 한 다음 배열을 만드는 것입니다.

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)