This is somewhat related to my previous post that got a nice reply from Dan, and it was nice to hear that it’s not just noob eyes that sometimes think that “pythony” syntax actually is more difficult to read than “basicy” (even though Python is lauded as an easy to read language, I often find it very obfuscated somehow).
I just completed a kata on codewars with the text to alphabet position, which went fine, the best practice solution was
return ' '.join(str(ord(c) - 96) for c in text.lower() if c.isalpha())
I’d forgotten about the “isalpha” method, but I’d tried a similar loop construct but it didn’t work:
for x in text.lower() if ord(x) in range(97,123)
so I went back to this style (which as before seems to be a bit easier to follow whats going on)
result = []
for x in text.lower():
if ord(x) in range(97, 123):
result.append(f"{str(ord(x)-96)} ")
return " ".join(result)
my question is really, what part of the syntax should I look up to fully understand where mine went wrong, is it the “in” part or is it the stuff that the “for” keyword is getting.
I see that in the best practice solution the extra bit in the for loop is a simple “if bool” whereas mine is a bit more long winded as “if… in…” bool, is that the problem that this is a full expression?
and actually why do neither of these work as a loop construct
for x in text.lower() if x.isalpha():
for x in text.lower() if x.isalpha()
when this works just fine in the optimal solution
return ' '.join(str(ord(c) - 96) for c in text.lower() if c.isalpha()
hmm, but then this worked fine in a subsequent kata
return sum(1 for x in arr if x in happy)
maybe in my very first question, if I’d done
span = [97,98,...etc..., 122]
for x in text.lower() if ord(x) in span
i.e. is it the range function iterating that caused the problem, I will check this
however, I am still not sure why the other example works as a single line loop in a return but you cannot use the same loop on its own as normal
no its definitely related to it being sent back via the return statement, so basically, what is the reference material I should read to understand how for loop constructions with conditionals can work as something in a return but not on its own