'필터'유형의 개체에 len ()이 없습니다. [중복]

Nov 16 2020

곡선 아래 면적을 계산하기 위해 몬테카를로의 시뮬레이션을 사용하려고합니다 (파이썬 3.8)

결국이 플롯을 얻으려면

나는이 코드를 시도했다

import random, math

NUM_POINTS = 10000

# Function for which we want to find area from (x=0 to 10).
f = lambda x: 5 * math.sin(6 * x) + 3 * math.sin(2 * x) + 7

# Sample rectangle will be (x,y) such that 0 <= x <= 10 and 0 <= y <= 14.
rect_width = 10
rect_height = 14

# Funcitions to generate samples for x and y respectively.
rand_x = lambda: random.uniform(0, rect_width)
rand_y = lambda: random.uniform(0, rect_height)

# Generate random sample points.
points = [(rand_x(), rand_y()) for i in range(NUM_POINTS)]

# Find points under our function
points_under = filter(lambda point: point[1] <= f(point[0]), points)

# Area = area of domain rectangle * num_points_under/num_points_total
area = rect_width * rect_height * len(points_under)*1.0/len(points)
print ("Estimate of area under the curve:", area)

이 오류가 있습니다.

'filter'유형의 객체에 len ()이 없습니다.

누구든지 제발 나를 도울 수 있습니까?

답변

Hello_world Nov 16 2020 at 03:24

감사합니다. 2.X 파이썬 코드이고 저는 3.X 버전의 파이썬을 사용하고 있기 때문에이 도구는 제 코드를 변환하는 데 도움이되었습니다. https://www.pythonconverter.com/