Python Forum
When did the number got included in the list?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
When did the number got included in the list?
#11
Quote: Anyway whats the point of having a function if you need an if statement to stop the generator.
First off, it is a generator, not a function. Functions return a value and they are done. Generators return a value and hold.

The point of a generator like this is you may not always know when a sequence should end. Lets say I want a sequence of prime numbers that add up to 100 or less. I don't know when my sequence should stop exactly. I can guess, but I don't know. I could write it like this:
primes = []
total = 0
for p in prime_numbers():
    if (total := total + p) > 100:
        break
    else:
        primes.append(p)
print(sum(primes), primes)
Output:
100 [2, 3, 5, 7, 11, 13, 17, 19, 23]
Or if you prefer a while loop.
primes = []
total = 0
prime_gen = prime_numbers()
while total < 100:
    p = next(prime_gen)
    if (total := total + p) <= 100:
        primes.append(p)
print(sum(primes), primes)
My example is a bit hokey, but so is the prime_numbers() generator in your example. I think this code is supposed to demonstrate using a generator and is not meant to be used as a prime number generator.

I agree that there is probably a little language barrier here. Some of your questions are a bit awkward, but I am only proficient in one language, so you are way ahead of me. This is far from "conversational English" and many native English speakers have trouble asking questions on this forum.
Reply
#12
Thank you snippsat very good example i'm gonna be keeping in my study book.
Dean last post was answering pretty much my questions. TY
Reply
#13
Quote:First off, it is a generator, not a function. Functions return a value and they are done. Generators return a value and hold.

But they still use the key word "def" and without the "yield" there is no generator. One would think they could of created some kind of method or class to represent the Generators.

Quote:This is far from "conversational English" and many native English speakers have trouble asking questions on this forum.

I would of think of non-native speakers. Anyway this forum is great. If you ask any questions on stackoverflow. If the question is not detailed or properly formulated they just kick you out after 3 questions.

French-canadian language is like Catalan with spanish not to far from french(France) and not to far from english(american).
Reply
#14
(Feb-02-2022, 07:29 PM)Frankduc Wrote: But they still use the key word "def" and without the "yield" there is no generator. One would think they could of created some kind of method or class to represent the Generators.
Can make a generator and not using a function,bye making a generator expression.
>>> squares = (n** 2 for n in range(5))
>>> squares
<generator object <genexpr> at 0x0000019FB47C6510>

>>> list(squares)
[0, 1, 4, 9, 16]
From outside of function there is also Iterator.
>>> lst = [1, 2, 3]
>>> it = iter(lst)
>>> it
<list_iterator object at 0x0000019FB48430A0>
>>> next(it)
1
>>> next(it)
2
>>> next(it)
3
>>> list(it)

>>> it = iter(lst)
>>> it
<list_iterator object at 0x0000019FB47AC040>
>>> list(it)
[1, 2, 3]
So work it similar way,in fact so a Generator a subclass of Iterator.

The main strategy of this is Lazy Evaluation.
A evaluation strategy which delays the evaluation of an expression until it's value is needed,
and also avoids repeated evaluations.

When moved from Python 2 to 3 was made a lot stuff was rewritten to use this strategy.
# Python 2.7
>>> range(5)
[0, 1, 2, 3, 4]

>>> zip(range(5), range(5))
[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)]
Python 2 return a finish made list.
# Python 3.10
>>> range(5)
range(0, 5)
>>> list(range(5))
[0, 1, 2, 3, 4]

 >>> zip(range(5), range(5))
<zip object at 0x0000019FB41B27C0>
>>> list(zip(range(5), range(5)))
[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)]
In Python 3 it's lazy evaluated,so have to iterate over it to get result or call list() or other function like sum().
>>> sum(range(5))
10
Reply
#15
I saw similar examples in https://www.programiz.com/python-programming/iterator
Generator are in curly brackets like a tuple but they are object that return a list.
I like the use you made of list (zip) much fater than while:

coordinate = []

y = 0
while y < 10:
    x = 0
    while x < 10:
        coordinate.append((x,y))
        x += 1
    coordinate.append((x,y))
    y += 1
    
for x in coordinate:
     print(x)
Thanks for the tips.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Delete strings from a list to create a new only number list Dvdscot 8 1,554 May-01-2023, 09:06 PM
Last Post: deanhystad
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,255 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  The included URLconf 'scribimus.urls' does not appear to have any patterns in it. nengkya 0 1,075 Mar-03-2023, 08:29 PM
Last Post: nengkya
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,888 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  Split a number to list and list sum must be number sunny9495 5 2,308 Apr-28-2022, 09:32 AM
Last Post: Dexty
  Divide a number by numbers in a list. Wallen 7 8,059 Feb-12-2022, 01:51 PM
Last Post: deanhystad
  Count number of occurrences of list items in list of tuples t4keheart 1 2,398 Nov-03-2020, 05:37 AM
Last Post: deanhystad
  How do I add a number to every item in a list? john316 2 1,987 Oct-28-2020, 05:29 PM
Last Post: deanhystad
  Print the number of items in a list on ubuntu terminal buttercup 2 1,957 Jul-24-2020, 01:46 PM
Last Post: ndc85430
  Make an array of string number in a List polantas 5 3,124 May-27-2020, 07:18 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020