0

Here in my code I am getting output something like <generator object <genexpr> and I dont know why. I tried to cast it to some tuple or list but still the same problem.

for example:
>>> 5 1000          //where n = 5 is number of points, p = 1000 the money which already exists
>>> 386 780 0       //x,y,m and we just care about m here and if m of any point is bigger than p, 
    494 160 1215    //we append the point to the list l
    313 573 1553
    216 506 750
    355 506 1630

output should be number of points '2 3 5' but I'm getting this:

<generator object <genexpr> at 0x012AB610> <generator object <genexpr> at 0x012AB5A0> <generator object <genexpr> at 0x012AB648>

My code:

import random
random.seed(42)
n, p = list(map(int, input().split()))
x = []
y = []
m = []
l = set()

for i in range(0, n):
    a1, a2, a3 = list(map(int, input().split()))
    x.append(a1)
    y.append(a2)
    m.append(a3)
points = [random.random() for _ in range(n)]
for v in m:
     if v > p:
         l.add(v(point) for point in points)

print(*l)
print(len(l))
3
  • You are using a generator expression to create generator objects... Do you know what that means? If not, I suggest not using generators Commented May 14, 2020 at 23:22
  • are you using a set on purpose too? Sets don't allow only unique values Commented May 14, 2020 at 23:27
  • @juanpa.arrivillaga yeah I know it's a generator. I am just trying to append those points which its m > p and I only found out this method. Commented May 14, 2020 at 23:30

2 Answers 2

2

v(point) for point in points is a generator expression. You don't get the output unless you do something that iterates over it.

You should iterate over it, and add each element.

for v in m:
    if v > p:
        for point in points:
            l.add(point)
Sign up to request clarification or add additional context in comments.

9 Comments

I've already tried that before but I get this error ``` l.add(tuple(v(point) for point in points)) TypeError: 'int' object is not callable```
That's a different problem. What is v(point) supposed to mean?
Maybe you meant v[point]? I can't really follow what your code is doing.
Sorry I did a mistake here, it should be point(v). it means the point of object v. I am trying to append those points which its m > p and I only found out this method. I would like to know if there's other useful methods
point(v) doesn't make sense either. point is a random number, not a function.
|
1

v(point) won't work because v is an integer, not a function. The expression v(point) for point in points is creating a generator object which is why it's not throwing an error.

It seems to me that you are trying to add the points for inputs where m is greater than p. You can do so by using enumerate:

import random
random.seed(42)
n, p = list(map(int, input().split()))
x = []
y = []
m = []
l = set()

for i in range(0, n):
    a1, a2, a3 = list(map(int, input().split()))
    x.append(a1)
    y.append(a2)
    m.append(a3)
points = [random.random() for _ in range(n)]
for idx, v in enumerate(m):
     if v > p:
         l.add(points[idx])

print(*l)
print(len(l))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.