nous vous remercions de votre aide à l'avance.Comment rechercher des sous-chaînes de manière récursive dans Python?
J'ai une liste de chaînes
full_name_list = ["hello all","cat for all","dog for all","cat dog","hello cat","cat hello"]
je dois faire un match pour cent entre chaque élément à tous les éléments de la liste. Par exemple, je dois d'abord décomposer "hello all"
en ["hello", "all"]
et je peux voir que "hello"
est en "hello cat"
donc ce serait une correspondance de 50%. Voici ce que j'ai jusqu'à présent,
hello all [u'hello', u'hello all', u'hello cat', u'cat hello'] [u'all', u'hello all', u'cat for all', u'dog for all']
cat for all [u'cat', u'cat for all', u'cat dog', u'hello cat', u'cat hello'] [u'for', u'cat for all', u'dog for all'] [u'all', u'hello all', u'cat for all', u'dog for all']
dog for all [u'dog', u'dog for all', u'cat dog'] [u'for', u'cat for all', u'dog for all'] [u'all', u'hello all', u'cat for all', u'dog for all']
cat dog [u'cat', u'cat for all', u'cat dog', u'hello cat', u'cat hello'] [u'dog', u'dog for all', u'cat dog']
hello cat [u'hello', u'hello all', u'hello cat', u'cat hello'] [u'cat', u'cat for all', u'cat dog', u'hello cat', u'cat hello']
cat hello [u'cat', u'cat for all', u'cat dog', u'hello cat', u'cat hello'] [u'hello', u'hello all', u'hello cat', u'cat hello']
Comme vous pouvez le voir le premier mot de chaque sous-liste contient la sous-chaîne qui est en cours de recherche suivi par les éléments qui contiennent cette sous-chaîne. Je suis capable de faire cela pour des correspondances d'un mot, et j'ai réalisé que je peux continuer ce processus en prenant simplement l'intersection entre des mots individuels pour obtenir des correspondances doubles, par ex.
cat for all [(cat,for) [u'cat for all']] [(for,all) [u'cat for all', u'dog for all']]
Le problème que j'ai est de le faire récursivement puisque je ne sais pas combien de temps ma chaîne la plus longue va être. Aussi, y a-t-il une meilleure façon de faire cette recherche de chaîne? En fin de compte, je veux trouver les chaînes qui correspondent à 100% parce que de façon réaliste "hello cat" == "cat hello"
. Je veux aussi trouver les 50% et ainsi de suite.
Une idée m'a été donnée en utilisant un arbre binaire, mais comment puis-je faire ça en python? Voici mon code à ce jour, je ne suis pas un programmeur et je sais que cela ne regarde pas si bon si nu avec moi:
logical_list = []
logical_list_2 = []
logical_list_3 = []
logical_list_4 = []
match_1 = []
match_2 = []
i = 0
logical_name_full = logical_df['Logical'].tolist()
for x in logical_name_full:
logical_sublist = [x]+x.split()
logical_list.append(logical_sublist)
for sublist in logical_list:
logical_list_2.append(sublist[0])
for split_words in sublist[1:]:
match_1.append(split_words)
for logical_names in logical_name_full:
if split_words in logical_names:
match_1.append(logical_names)
logical_list_2.append(match_1)
match_1 = []
logical_list_3.append(logical_list_2)
logical_list_2 = []
C'est cool, j'ai juste besoin de savoir comment utiliser chaque mot dans ma liste à la place de la variable « mot », puis imprimer tout en œuvre pour exceller afin que je puisse voir une visualisation de tous les pour cent des correspondances. Merci de m'avoir donné cette autre idée de l'idée parce qu'au lieu de parcourir chaque élément des mots entiers et de les apparier un à un à d'autres mots complets, je pourrais juste voir comment chaque mot complet se rapporte à d'autres mots complets. –
@EdwardMordechay Vous pouvez simplement prendre votre tableau et .joindre ("") dans le mot variable. –
@EdwardMordechay Et pour visualiser à exceller, vous pouvez simplement utiliser csv. Dans ce cas, votre fichier results.append ressemblera à ceci: results.append (i + "," + str ((match_words/total_words) * 100) + "%") Vous aurez besoin d'un fichier IO par la suite. –