2012-04-09 86 views
5

定義一個過程,same_structure,需要兩個輸入。如果列表具有相同的結構,則應該輸出 True,否則應該輸出False 。兩個值,p和q具有相同的結構,如果:如何找出兩個python中具有相同結構的列表?

Neither p or q is a list. 

Both p and q are lists, they have the same number of elements, and each 
element of p has the same structure as the corresponding element of q. 

編輯:爲了使畫面清晰,以下是預期輸出

same_structure([1, 0, 1], [2, 1, 2]) 
    ---> True 
same_structure([1, [0], 1], [2, 5, 3]) 
    ---> False 
same_structure([1, [2, [3, [4, 5]]]], ['a', ['b', ['c', ['d', 'e']]]]) 
    ---> True 
same_structure([1, [2, [3, [4, 5]]]], ['a', ['b', ['c', ['de']]]]) 
    ---> False 

我想遞歸將是最好的解決這個問題python我已經拿出下面的代碼,但它不工作。

def is_list(p): 
    return isinstance(p, list) 

def same_structure(a,b): 
    if not is_list(a) and not is_list(b): 
     return True 
    elif is_list(a) and is_list(b): 
     if len(a) == len(b): 
      same_structure(a[1:],b[1:]) 
    else: 
     return False 
+0

@SvenMarnach:除非我誤讀的問題,[2,(3,4)]和[2,(5)具有相同的結構:他們是兩個列表,它們具有相同的元素的數量,並且p的每個元素都具有與q的對應元素相同的結構,因爲它們都不是列表。 IOW它只是重要的列表結構,值不會(因此[2]和[3]也具有相同的結構。) – DSM 2012-04-09 15:20:33

+1

爲列表定義「相同的結構」,與python ['a','1 ']不等於['1','a']。也許你會更好地使用集合。 – KurzedMetal 2012-04-09 15:24:20

+0

在這種情況下,儘管我認爲我*會*說他們包含相同的元素,其中的平等是由給定的關係定義的。我想我自己在這一天有過這樣的問題。 – DSM 2012-04-09 15:37:54

回答

3

遞歸是一個好主意,但沒有給你建議的方式。首先(這可能只是一個錯字),你實際上並沒有回到這裏什麼:

if len(a) == len(b): 
    same_structure(a[1:],b[1:]) 

其次,你應該遞歸每一個元素,而不是每個子列表處理。即:

if len(a) == len(b): 
    for i in range(len(a)): 
     if not same_structure(a[i], b[i]): 
      return False 
    return True 
else: 
    return False 

希望這會有所幫助。

5

你錯過了一個案例,並忘記返回第二個案例。注意,沒有必要明確比較列表的長度,因爲第一種情況需要處理 - 如果其中一個列表是空的而另一個不是,那是因爲一個列表的元素少於另一個:

def same_structure(a, b): 
    if a == [] or b == []: # one of the lists is empty 
     return a == b  # are both of the lists empty? 
    elif is_list(a[0]) != is_list(b[0]): 
     return False  # one of the elements is a list and the other is not 
    elif not is_list(a[0]): # neither element is a list 
     return same_structure(a[1:], b[1:]) 
    else:     # both elements are lists 
     return same_structure(a[0], b[0]) and same_structure(a[1:], b[1:]) 
+0

感謝您的答案,但由於第二種情況下的整數比較而無法正常工作 – u449355 2012-04-09 15:35:53

+0

請看您期望的預期輸出結果。 – u449355 2012-04-09 15:36:13

+0

@ UmeshKacha第二個條件?,你們這樣做:'is_list(a [0])!= is_list(b [0])''。這不是一個整數比較,它是一個布爾比較。這句話說的是:「如果其中一個元素是一個列表而另一個不是一個列表,那麼返回False,因爲結構不同」 – 2012-04-09 15:44:20

5

相反的same_structure(a[1:],b[1:]),你需要逐個

def is_list(p): 
    return isinstance(p, list) 

def same_structure(a, b): 
    if not is_list(a) and not is_list(b): 
     return True 
    elif (is_list(a) and is_list(b)) and (len(a) == len(b)): 
     return all(map(same_structure, a, b)) # Here 
    return False 
1

檢查項目對a和b由於該規範指出,輸入是兩個列表,你可以重複你的函數裏面的名單沒有進一步如果您遇到子列表,只會執行遞歸調用:

def same_structure(a, b): 
    if len(a) != len(b): 
     return False 
    return all(is_list(x) and is_list(y) and same_structure(x, y) or 
       not is_list(x) and not is_list(y) 
       for x, y in zip(a, b)) 
0

你可以試試這個方法也同樣,檢查兩個列表類型&長度兩份名單 和兩個列表的子列表做遞歸。

def same_structure(a,b): 
    return isinstance(a, list) and isinstance(b, list) and len(a) == len(b) 
    and all(same_structure_as(c, d) for c, d in zip(a, b) if isinstance(c, list)) 
+0

雖然這段代碼可能會回答這個問題,但提供關於如何解決問題和/或爲什麼解決問題的其他上下文會提高答案的長期價值。 – 2017-02-13 15:59:26

相關問題