0
I don't understand this example of set comprehension in python
printing this set a={j for x in range(10) for j in range(x)} outputs {0,1,2,3,4,5,6,7,8} however I don't understand why. Can someone please explain?
11 Respuestas
+ 6
That's a trapped comprehensive set, not improved at all ( probably design for challenge purpose :P ):
If you write it to be a comprehensive list instead of set, your output will be:
[0,0,1,0,1,2,0,1,2,3,0,1,2,3,4,0,1,2,3,4,5,0,1,2,3,4,5,6,0,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7,8]
... which will be your outputed set once reduced ( a set is a list with unique items )
You can start to better understand what occurs during the comprehensive expression excution.
To achieve this, develop the comprehensive expression:
for x in range(10):
for j in range(x):
print(j) # each print is a new item add to the list/set
Ten times, you will set a new range of items. But on first loop, the range is null ( with x = 0 the nested loop is never executed ).
Next loop you will add 0, then 0 and 1, next 0, 1, 2... and so on until 10 not included, so last nested loop will return each items of range(9) wich is digits from 0 to 8 included...
To convince yourself if needed, try to run this:
for x in range(10):
for j in range(x):
# each print is formatted as x ( j ) with j the new item add to the list/set
print(x,' (',j,')')
+ 2
Adding spaces is like changing variable names... it's just throwing powder in the eyes ^^
I am convinced that @Nadav is one of the numbered sololearns who are hunting bagdes and XP at any prices, and that this is an aborted attempt to get the Self-Learner badge, and maybe if I don't have react will him mark itself its answer as best one :P
So... It's the community who will be looser from that: personally, I went to take care of some things else, real life and my own codes, having a Sololearn break from about 12 hours... and I will get some retreat to waste less time in helping not respectful people :(
+ 1
Five minutes for understanding and copying my answer ^^
+ 1
I don't really believe you, even if I let you the doubt benefit:
You've posted your question about 2 hours sooner, and just five minutes after my time spending answer, you got it ( Eureka! ), with exactly the same process of explanation ^^ ( just changing variable names, but even not logical as keeping the one of your question code ).
So, it doesn't matter; I'm just day after day more desabused, and will probably soon stop attempt to share help/stuff here :(
+ 1
Visph I can't tell you if Nadav's lying or not, but I doubt he would'be copied your list output and then bother to run through it adding spaces following each comma. It may seem unlikely that you both arrived at the same conclusion, but when the answer is the right one is it actually surprising he found it?
There's been some copying and bumholery on here of recent, but that doesn't mean everyone's up to no good. : )
0
lol I saw your answer just now, after submitting my own. But thanks anyway, now I'm sure I got this right.
0
yes I posted this two hours ago and left since I had to get away from my desktop. But I kept thinking about it and returned. I refreshed the page and saw no new answers so I changed the set to a list and then I figured it out. I posted my answer and to my surprise there was another answer, submitted just minutes before. I am grateful for your help, and I find it odd that you think I copied my answer from yours. What would I gain from that? Especially when the two answers are right there on the page, it would be funny to copy an answer, when the original is right beside it. So no, I did not copy your answer, and I appreciate the time and effort you took to submit it, which is why I upvoted it. I'm only here to learn and get better at coding, copying answers would be just a waste of my time. I consider the whole incident a somewhat weird and funny ancedote and that's it.
0
Sorry to hear that. I hope things improve for you Visph
0
I'm trying to be nice and civilized yet @visph keep insisting that I'm a liar and "one of the numbered sololearns who are hunting bagdes and XP at any prices".
I couldn't care less about upvotes, badges, xp or any imaginary arbitrary number. My only goal is to get better as a coder. I don't know you and you don't know me but unlike you I don't make claims about your intentions or personality.
Yesterday I failed a question at a challenge and decided to ask for help in the forums, for the first time. I didn't know it will end in me being labeled and called names. It's funny how you say you're disappointed in the community yet I'm the one who now will avoid posting anything, especially if, god forbid, I figure out the answer on my own.
Let me say it one final time, so it's clear: I didn't copy a single bit from your answer.
Arguing with strangers over the internet over things I never did is profoundly unproductive, so this is my final post in the matter.
In the legendary words of Mr. Gene Wilder: good day sir.
0
I think recent events have raised tensions, but you both need to realise that most of the damage is being done to the SoloLearn community. If you are both interested in being the best coders you can be then I suggest you "shake and be friends" because jumping to hostility is far too easy. Try to understand that we all have to deal with the negative side of the community, let's not add to it.
Nadav, you want to be able to ask for help in future right?
Visph, you want to help others in future right?
Forget the damn badges and all the bullship and talk code. THAT is all that matters here. Leave the fighting to others.
Visph, in teaching others you better your own understanding and (even) if your accusation against Nadav were true, he'd be failing himself by not learning so it's meaningless. If you improve your understanding and he cheats then you win in that situation and you would help anyone else who reads your response.
TL;DR
Let's work together, not fight : )
- 1
I think I figured this out. When changing the type from set to list, the content of the list is:
[0, 0, 1, 0, 1, 2, 0, 1, 2, 3, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 8]
so this is actually a nested loop. It is the same as:
list = []
for l in range(10):
for k in range(l):
list.append(k)
the content of list would be the same:
list = [0, 0, 1, 0, 1, 2, 0, 1, 2, 3, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 8]
However since the original type is a set, not a list, then there are no repetitions and this is how we get {0,1,2,3,4,5,6,7,8}.
edit: submitted my answer only to find someone else submitted theirs just few minutes before mine lol