2014-11-23 26 views
-1

我想要如何從列表列表中找到偶數。
的例子是:在列表中找到偶數列表python

maximum_even_each_list_in_lol([[1,2,3],[6,5,4],[5,7,9]]) 

應該返回(最大偶數):

[2,6,0] 

我發現如何找到甚至從列表編號,從計算器:

def maximum_even_each_list_in_lol (lol): 
    evens=[] 
    for number in lol: 
     if is_even(number): 
      evens.append(number) 
    return evens 

,但我想知道如何讓它可以從列表中的每個列表中找到偶數。

+0

從哪裏這0來自? – Hackaholic 2014-11-23 22:52:03

+0

你從哪裏得到0? – 2014-11-23 22:52:06

+0

和爲什麼4沒有出現? – Hackaholic 2014-11-23 22:55:12

回答

3

雖然你沒有明確地說出來,但似乎你正在尋找列表中每個列表中的最大偶數。從你的例子看來,如果沒有偶數,程序似乎要加0。在這種情況下,這將工作:

print [max(value for value in [0] + L if not value % 2) for L in list_of_lists] 
0

你幾乎在那裏! :-)

檢查內置函數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

據我瞭解,你要遍歷列表的列表,抓住最大的甚至每個列表0號或者如果沒有被發現偶數。這裏是你的函數,那麼:

def maximum_even_each_list_in_lol(lol): 
    evens = [] 
    for sublist in lol: 
     subevens = [] 
     for number in sublist: 
      if (not number % 2): 
       subevens.append(number) 
     # As I understand you want to insert 0 
     # if no even numbers were found, right? 
     if not subevens: 
      subevens = [0] 
     evens.append(max(subevens)) 
    return evens 

# Testing 
assert maximum_even_each_list_in_lol([[1,2,3],[6,5,4],[5,7,9]]) == [2,6,0] 

請務必閱讀內建max功能:)

2

您可以從內到外採用一些不錯的Python的語言結構,建立這個算法起來:

# Function to determine if one number is even 
is_even = lambda x : x%2==0 

# Function to return just the even numbers of a list 
just_evens = lambda x : filter(is_even, x) 

# Function to return the maximum number in a list, or '0' if there is none 
get_max = lambda x : max(x) if len(x) else 0 

# Function to return the maximum even number of a list 
max_even = lambda x : get_max(just_evens(x)) 

# Print out the maximum even number from each list: 
map(max_even, [[1,2,3],[6,5,4],[5,7,9]]) 

這將打印出:

[2, 6, None] 
+0

對於0部分,我不知道如何,但我的教授把0作爲返回值,所以我自己對這個問題有點困惑。 – Dennis 2014-11-23 23:19:36

+0

OP規範中的示例結果具有零而不是'None'。否則,我真的很感激你的答案。 – gboffi 2014-11-23 23:20:26

+0

更新爲默認爲'0'。 – 2014-11-24 00:15:47

0

在這裏你去:

>>> [ max(x) if len(x)>0 else 0 for x in [ list(filter(lambda x:x%2==0,x)) for x in a ]] 
[2, 6, 0] 

我用lambda來檢查偶數,然後過濾,以節省值true,則最多如果列表lenght大於0,否則,0

1

python 3.4花費最大的默認值,所以如果濾波後有沒有偶數我們可以返回0作爲默認值:

l = [[1,2,3],[6,5,4],[5,7,9]] 
print([max(filter(lambda x: not x % 2,x),default=0) for x in l]) 
[2, 6, 0] 
+1

Python 3.4+是精確的。 :-) – 2014-11-23 23:44:43

+0

@AshwiniChaudhary,yep忘了添加鏈接 – 2014-11-23 23:50:59