私は以下のリストを持っています。私は基本的には問題の人と現在友人ではないすべての人を意味する潜在的な友人のリストを作成しようとしています。辞書からの潜在力のリスト
辞書のキーは人物であり、値のリストはその友人です。
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)あなたの 'person_to_friends'の例のリストの名前から来るのでしょうか、(2)person_to_friendsの例では、その人のキー値のペアリストにないその辞書のすべての名前からの潜在的な友人です。 (3)潜在的な友人は別のリストから来るのですか? –
@Abbyがあなたのために働くなら、私の解決策をチェックしてください。 –