如何使用Python代码查找关联 [英] How to find associations using Python code
问题描述
我有一个元组:[[G,E],[B,F],[G,F],[G,D],[D,E],[H],[B,E],[ E,F],[I]]
您是否有关于如何编写python代码以识别每个字母的其他字母关联的想法。例如,对于E,我们需要找到G,D,B,F因为它们与E相关联。感谢您的帮助。
我有什么尝试过:
我有一个元组:[[G,E],[B,F],[G,F],[G,D ],[D,E],[H],[B,E],[E,F],[I]]
你对如何有想法编写一个python代码来识别每个字母的其他字母的关联。例如,对于E,我们需要找到G,D,B,F,因为它们与E相关联。感谢您的帮助。
I have a tuple: [[G, E], [B, F], [G, F], [G, D], [D, E], [H], [B, E], [E, F], [I]]
Do you have ideas about how to write a python code to identify the association of other letters for each letter. For example, for E, we need to find G, D, B, F because they are associate with E. Appreciate your help.
What I have tried:
I have a tuple: [[G, E], [B, F], [G, F], [G, D], [D, E], [H], [B, E], [E, F], [I]]
Do you have ideas about how to write a python code to identify the association of other letters for each letter. For example, for E, we need to find G, D, B, F because they are associate with E. Appreciate your help.
推荐答案
尝试
Try
l = [['G', 'E'], ['B', 'F'], ['G', 'F'], ['G', 'D'], ['D', 'E'], ['H'], ['B', 'E'], ['E', 'F'], ['I']]
r = []
for sl in l:
count = sl.count('E')
if count > 0:
[r.append(c) for c in sl if c != 'E']
print(r)
顺便说一下,平方括号数据结构是 list
s,而不是元组
s。
谢谢你的答案,这有助于找到E的关联,所有其他如何,如G,B,D,I,F等。有没有办法使用哈希表这个?谢谢。
Thank you for the answer, this helps finds the association for E, how about all others, such as G, B, D, I, F, etc. Is there a way to use Hash table for this? Thank you.
这篇关于如何使用Python代码查找关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!