0
Can anybody explain why i can use in expression and not jn normal way
I am super confuse https://code.sololearn.com/ccl4ctqPmM60/?ref=app
2 Respostas
+ 2
# Hi, Aisy Danish Mohd Nazry !
# In your first code example you created a generator expression inside min(), that the min-function works over.
# So in your second example, you have to do this work by your self.
# To to illustrate this, I put all word lengths in a list:
def find_short(s):
res = []
for x in s.split():
res.append(len(x))
return min(res)
print(find_short("hshshsnsjsjsj js jsjs"))
# Your first code example is acctually a short way to doing this (but
# this of course nobody does, because the min-function handle it for you):
def find_short(s):
# Create a generator.
def wd_lengths(s):
for x in s.split():
yield len(x)
gen = wd_lengths(s)
return min(gen)
txt = "hshshsnsjsjsj js jsjs"
print(find_short(txt))
+ 1
Thank you bro 💪💪🫶🏻