Iteration, while loop, and for loop
Grant Edwards
grant.b.edwards at gmail.com
Tue Jun 28 13:58:52 EDT 2016
On 2016-06-28, Tim Chase <python.list at tim.thechases.com> wrote:
> On 2016-06-29 01:20, Steven D'Aprano wrote:
>> While loops are great for loops where you don't know how many
>> iterations there will be but you do know that you want to keep
>> going while some condition applies:
>>
>> while there is still work to be done:
>> do some more work
>
> I find this particularly the case when the thing being iterated over
> can be changed, such as a queue of things to process:
>
> items = deque()
> items.append(root_node)
> while items:
> item = items.popleft()
> process(item)
> items.extend(item.children)
Yep, I often do something similar when processing a block of data
bytes comprising a sequence of "things" of varying number of bytes.
data = read_a_blob_of_bytes()
while data:
#figure out how long the first "thing" is
len = <some expression typically involving the first few bytes of 'data'>
handle_thing(data[:len])
data = data[len:]
> But then, if you wrap up your "while" loop as a generator that yields
> things, you can then use it in a "for" loop which seems to me like
> the Pythonic way to do things. :-)
Yea, I keep telling myself that, but I never actually do it.
--
Grant Edwards grant.b.edwards Yow! How do I get HOME?
at
gmail.com
More information about the Python-list
mailing list