Hi, I report error at the https://docs.python.org/3/library/functools.html#functools.reduce specifically, at “If the optional initializer is present, it is placed before the items of the sequence in the calculation, and [serves as a default when the sequence is empty]. If initializer is not given and sequence contains only one item, the first item is returned. (bracket is added by me for emphasis)” Actually, it should be stated as ‘serves as a default to be run before the lambda reads the given sequence.’ For example, at the code def string_to_int(s): return functools.reduce(lambda running_sum, c: running_sum * 10 + string.digits.index(c), s[s[0] == '-':], 0) * (-1 if s[0] == '-' else 1) t he 0 does the job setting running_sum, c even before it traverses the given string, not restricted to the empty string case. Precisely, if we have s = ' -324 ' , we need to have ru nning_sum=0, c=0 initially, to make it (0*10+index(0)) … as the running_sum cannot be str, if so I get the initializer does the job of setting all the argument of lambda: function as initializer. Please consider this. Thanks. Best regards, Dongkyu Choi +1 (929) 302-9410 creditchoi@gmail.com
participants (1)
-
Dongkyu Choi