2017-12-10 17 views
0

2つの入力を受け入れる "common_ancestor()"という関数を作成しようとしています。最初は文字列タクサ名のリスト、2番目は系統樹辞書です。入力リストにあるすべての 種の中で、最も近い共通の祖先である分類群の名前を示す文字列を返します。 list_ancestorsという別の関数を既に作成しています。この関数はリストの要素の一般的な先祖です。また、私が働いている辞書を持っています。Pythonでネストされたツリー辞書のcommon_ancestor関数

tax_dict = { 
'Pan troglodytes': 'Hominoidea',  'Pongo abelii': 'Hominoidea', 
'Hominoidea': 'Simiiformes',   'Simiiformes': 'Haplorrhini', 
'Tarsius tarsier': 'Tarsiiformes',  'Haplorrhini': 'Primates', 
'Tarsiiformes': 'Haplorrhini',   'Loris tardigradus':'Lorisidae', 
'Lorisidae': 'Strepsirrhini',   'Strepsirrhini': 'Primates', 
'Allocebus trichotis': 'Lemuriformes', 'Lemuriformes': 'Strepsirrhini', 
'Galago alleni': 'Lorisiformes',  'Lorisiformes': 'Strepsirrhini', 
'Galago moholi': 'Lorisiformes' 
} 

def halfroot(tree): 
    taxon = random.choice(list(tree)) 
    result = [taxon] 
    for i in range(0,len(tree)): 
     result.append(tree.get(taxon)) 
     taxon = tree.get(taxon) 
    return result 


def root(tree): 
    rootlist = halfroot(tree) 
    rootlist2 = rootlist[::-1] 
    newlist = [] 
    for e in range(0,len(rootlist)): 
     if rootlist2[e] != None: 
     newlist.append(rootlist2[e]) 
    return newlist[0] 


def list_ancestors(taxon, tree): 
    result = [taxon] 
    while taxon != root(tree): 
     result.append(tree.get(taxon)) 
     taxon = tree.get(taxon) 
    return result 

def common_ancestors(inputlist,tree) 
    biglist1 = [] 
    for i in range(0,len(listname)): 
     biglist1.append(list_ancestors(listname[i],tree)) 
     "continue so that I get three separate lists where i can cross reference all elements from the first list to every other list to find a common ancestor " 

結果は

一つの方法は、それぞれの種のためのすべての祖先を集めるセットでそれらを配置し、彼らが持っているものを得るために交差点を取得することです
print(common_ancestor([’Hominoidea’, ’Pan troglodytes’,’Lorisiformes’], tax_dict) 
    Output: ’Primates’" 
+1

ようこそStackOverflow。まず、あなたが試したこと、そしてあなたが不足しているところを示してください。ヘルプについては、[How to ask](https://stackoverflow.com/help/how-to-ask)をご覧ください。 –

+0

Homework Helpのように疑わしいように見えます。 – Quirk

答えて

0

のようになります共通:

def common_ancestor(species_list, tree): 
    result = None # initiate a `None` result 
    for species in species_list: # loop through each species in the species_list 
     ancestors = {species} # initiate the ancestors set with the species itself 
     while True: # rinse & repeat until there are leaves in the ancestral tree 
      try: 
       species = tree[species] # get the species' ancestor 
       ancestors.add(species) # store it in the ancestors set 
      except KeyError: 
       break 
     # initiate the result or intersect it with ancestors from the previous species 
     result = ancestors if result is None else result & ancestors 
    # finally, return the ancestor if there is only one in the result, or None 
    return result.pop() if result and len(result) == 1 else None 

print(common_ancestor(["Hominoidea", "Pan troglodytes", "Lorisiformes"], tax_dict)) 
# Primates 

あなたも、list_ancestors()のために、この機能の「真ん中」の部分を使用することができます - 何もNEがありません先祖の一部が自分自身やチェーンの破損がある場合に再帰した場合 - の両方が有効な先祖ツリー辞書に依存している、もちろん

def list_ancestors(species, tree, include_self=True): 
    ancestors = [species] if include_self else [] 
    while True: 
     try: 
      species = tree[species] 
      ancestors.append(species) 
     except KeyError: 
      break 
    return ancestors 

:ツリーのルートを見つけることを試みることによって、それを複雑にする編それは動作しません。また、これらの操作をたくさんする場合は、フラットな辞書を適切なツリーにする価値があります。

関連する問題