2017-11-20 14 views
0

私は以下のリストを持っています。私は基本的には問題の人と現在友人ではないすべての人を意味する潜在的な友人のリストを作成しようとしています。辞書からの潜在力のリスト

辞書のキーは人物であり、値のリストはその友人です。

person_to_friends = {'Jay Pritchett': ['Claire Dunphy', 'Gloria 
Pritchett', 'Manny Delgado'], 'Claire Dunphy': ['Jay Pritchett', 
'Mitchell Pritchett', 'Phil Dunphy'], 'Manny Delgado': ['Gloria 
Pritchett', 'Jay Pritchett', 'Luke Dunphy'], 'Mitchell Pritchett': 
['Cameron Tucker', 'Claire Dunphy', 'Luke Dunphy'], 'Alex Dunphy': 
['Luke Dunphy'], 'Cameron Tucker': ['Gloria Pritchett', 'Mitchell 
Pritchett'], 'Haley Gwendolyn Dunphy': ['Dylan D-Money', 'Gilbert D- 
Cat'], 'Phil Dunphy': ['Claire Dunphy', 'Luke Dunphy'], 'Dylan D- 
Money': ['Chairman D-Cat', 'Haley Gwendolyn Dunphy'], 'Gloria 
Pritchett': ['Cameron Tucker', 'Jay Pritchett', 'Manny Delgado'], 'Luke 
Dunphy': ['Alex Dunphy', 'Manny Delgado', 'Mitchell Pritchett', 'Phil 
Dunphy']} 

これまでのコードはここにありますが、潜在的な友人のリストをどのように更新するべきかはわかりません。

def friends_score(person, person_to_friends): 
    score = 0 
    potential_friends = [] 
    for item in person_to_friends: 
     if item == person: 
      potential_friends = #this is where I am unsure as to how to proceed 

FYI、潜在的な友人の定義は、その人が現在友人ではない人です。 [「クレアDunphy」、「グロリア プリチェット」、「マニー・デルガド」]

期待される成果:ジェイ・プリチェットは人だった場合、潜在的な友人は、値のリストの中の人を除いて全員だろう

def friends_score(person, person_to_friends): 
    # ... 
    potential_friends = [ 
     p for p in person_to_friends if p != person and p not in person_to_friends.get(person, []) 
    ] 
    # ... 

友人にないdictのすべてのキーを収集します。あなたは次のcomprehensionを使用することができ

>>> friends_score('Jay Pritchett') 

['Mitchell Pritchett', 'Phil Dunphy', 'Luke Dunphy', 'Cameron 
    Tucker', 'Alex Dunphy','Haley Gwendolyn Dunphy','Dylan D-Money', 
    'Gilbert D-Cat','Chairman D-Cat'] 
+1

がために期待される出力に含まを投稿1つのサンプル。 –

+0

私は同意します、潜在的な友人のあなたの説明は、それらの名前がどこから来たのか明らかではないので、出力として期待するものを投稿してください。すなわち、(1)あなたの 'person_to_friends'の例のリストの名前から来るのでしょうか、(2)person_to_friendsの例では、その人のキー値のペアリストにないその辞書のすべての名前からの潜在的な友人です。 (3)潜在的な友人は別のリストから来るのですか? –

+0

@Abbyがあなたのために働くなら、私の解決策をチェックしてください。 –

答えて

1

指定された人のリストであり、人自身ではありません。すべての潜在的な友人があなたにもセットを使用することができ、辞書のキーの場合には、

def friends_score(person, person_to_friends): 
    # ... 
    friends = person_to_friends.get(person, []) 
    # for larger data sets, you might want to convert the friends to a set 
    # friends = set(friends) 
    potential_friends = [] 
    for p in person_to_friends: 
     if p != person and p not in friends: 
      potential_friends.append(p) 
    # ... 
+0

これは良い解決策のように見えますが、すべての潜在的な友人が 'person_to_friends'ハッシュ(辞書)から' keys'としてリストされることを前提としています。これが本当であれば*(OPからはわかりません)*、これは良い解決策です。しかし、潜在的な友人が 'value'リストにリストアップされ、' key'ではない場合、これは改訂されるべきです。 –

+0

@DavidJohnColemanII "辞書の鍵は人物です" - 私の前提はすべてそこにあるということです。 – schwobaseggl

+0

この解決方法では、その人の自己をpotential_friendsリストに追加します。それは削除する必要があります。 –

0

def friends_score(person, person_to_friends): 
    s_fr = set(person_to_friends[person]) 
    s_fr.add(person) 
    s_all = set(person_to_friends.keys()) 
    return list(s_all.difference(s_fr)) 

output = friends_score('Jay Pritchett', person_to_friends) 

s_frは、本人とのセットであると:それは、次のとほぼ同等です彼らの友人。 s_allは、辞書のキーのすべての人物のリストです。最終的なリストは、2つのセットの違いから作成されます。

この同じソリューションでは、ディクショナリ値のみに含まれている人を考慮する必要がある場合に備えて、リストの理解とリストの平滑化を回避することはできません。その場合、@ schwobasegglソリューションは私にとっては良いようです。

0

あなたはセットを使用して関数内で1行に仕事をすることができます。

return list(set([i for i in person_to_friends])-set(person_to_friends.get(person))) 

全コード:

def friends_score(person, person_to_friends): 
    return list(set([i for i in person_to_friends])-set(person_to_friends.get(person))) 

print(friends_score('Jay Pritchett',person_to_friends)) 

出力:

['Dylan D-Money', 'Phil Dunphy', 'Cameron Tucker', 'Luke Dunphy', 'Mitchell Pritchett', 'Alex Dunphy', 'Jay Pritchett', 'Haley Gwendolyn Dunphy'] 
関連する問題