-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Language feature: list, set, dict comprehensions #6
Comments
Did this one too :-) Will make a PR. |
I'm already making good progress with the dict and list comprehensions. I ran into the fact that the list comprehensions can be repeated: text = (("Hi", "Steve!"), ("What's", "up?"))
print([word for sentence in text for word in sentence]) I checked the ast python generates: Module(
body=[
Assign(
targets=[
Name(id='text', ctx=Store())],
value=Tuple(
elts=[
Tuple(
elts=[
Constant(value='Hi'),
Constant(value='Steve!')],
ctx=Load()),
Tuple(
elts=[
Constant(value="What's"),
Constant(value='up?')],
ctx=Load())],
ctx=Load())),
Expr(
value=ListComp(
elt=Name(id='word', ctx=Load()),
generators=[
comprehension(
target=Name(id='sentence', ctx=Store()),
iter=Name(id='text', ctx=Load()),
ifs=[],
is_async=0),
comprehension(
target=Name(id='word', ctx=Store()),
iter=Name(id='sentence', ctx=Load()),
ifs=[],
is_async=0)]))],
type_ignores=[]) My problem is that I am struggling to make the |
What problem are you facing with the generators list? For list and dict comprehensions we shouldn't need generators just yet. |
For the case
The first generator is [for sentence in text] If the interpreter handles this correctly it should:
Now two 2 generators this might seem easy but what if there are 3 or 4 etc. I am not sure how to implement that. |
Sorry for delaying the response, I've been away for a while.
It is possible to do this part without generators as well! I'll try to
produce some example code, but that will be after Monday.
Thanks again!
…On Mon, Dec 4, 2023, 6:57 PM Jacco Kulman ***@***.***> wrote:
For the case
[word for sentence in text for word in sentence]
The first generator is [for sentence in text]
The second generator is [for word in sentence]
If the interpreter handles this correctly it should:
1. get an item from the first generator
2. yield items from the item fetched until those are finished then
repeat step 1
Now two 2 generators this might seem easy but what if there are 3 or 4 etc.
I am not sure how to implement that.
—
Reply to this email directly, view it on GitHub
<#6 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AKLGU44QDJ54ZNBPM2JLWNDYHW3DBAVCNFSM6AAAAAA3S27DV6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQMZYGQ4DKOJRGA>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Current implementation supports lists, sets and dicts, but it doesn't support their comprehensions.
Code like:
The text was updated successfully, but these errors were encountered: