你幾乎在那裏! :-)
檢查內置函數max的功能。
def maximum_even_each_list_in_lol (lol):
evens=[0] # Dirty trick to ensure that regardless of the `even` numbers
# found, you'll always have a zero among the results.
for number in lol:
if is_even(number):
evens.append(number)
print "I found these even numbers (%s) among %s" % (evens, lol)
return max(evens) # Return the maximum value among the `evens` list.
results = []
for lst in [[1,2,3],[6,5,4],[5,7,9]]:
print "Studying %s" % lst
results.append(maximum_even_each_list_in_lol(lst))
print "This is what I found: %s" % results
我加了一些print
語句來幫助遵循什麼代碼的作用。上面會輸出代碼:
Studying [1, 2, 3]
I found these even numbers ([0, 2]) among [1, 2, 3]
Studying [6, 5, 4]
I found these even numbers ([0, 6, 4]) among [6, 5, 4]
Studying [5, 7, 9]
I found these even numbers ([0]) among [5, 7, 9]
This is what I found: [2, 6, 0]
從哪裏這0來自? – Hackaholic 2014-11-23 22:52:03
你從哪裏得到0? – 2014-11-23 22:52:06
和爲什麼4沒有出現? – Hackaholic 2014-11-23 22:55:12