미리 도움을 주셔서 감사합니다.어떻게 파이썬에서 부분 문자열을 재귀 적으로 검색합니까?
나는 문자열
내가 목록의 모든 요소에 각 요소의 퍼센트 일치 할 필요가full_name_list = ["hello all","cat for all","dog for all","cat dog","hello cat","cat hello"]
의 목록을 가지고있다. 예를 들어, 먼저 "hello all"
을 ["hello", "all"]
으로 분해해야하며 "hello"
은 "hello cat"
에 있으며 따라서 50 % 일치합니다. 각 하위 목록의 첫 번째 단어는 문자열 것을 포함하는 요소 다음에 검색되는 문자열을 포함 볼 수 있듯이 여기
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']
, 내가 지금까지있는 것입니다. 한 단어 일치에 대해이 작업을 수행 할 수 있으며 개별 단어를 교차하여 간단하게 일치 항목을 가져 와서이 과정을 계속할 수 있음을 깨달았습니다.
cat for all [(cat,for) [u'cat for all']] [(for,all) [u'cat for all', u'dog for all']]
문제점 가장 긴 문자열의 길이를 알 수 없으므로이 문제는 재귀 적으로 발생합니다. 또한이 문자열 검색을 수행하는 더 좋은 방법이 있습니까? 궁극적으로 현실적으로 일치하는 문자열을 찾으려고합니다. "hello cat" == "cat hello"
. 나는 또한 50 % 일치를 찾고 싶다.
내가받은 아이디어는 이진 트리를 사용하는 것이지만 어떻게 파이썬으로이 작업을 수행 할 수 있습니까? 여기 내 코드는 내가 프로그래머 아닙니다, 지금까지 내가이 저와 너무 베어 그렇게 좋아 보이지 않습니다 알고
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 = []
이 멋지다, 난 그냥 모두의 시각화를 볼 수 있도록 엑셀 인쇄 다음이 모두 밖으로 변수 "단어"대신에 내 목록의 각 단어를 사용하는 방법을 알아낼 필요 퍼센트 일치. 전체 단어의 각 구성 요소를 하나씩 다른 단어로 매칭하는 대신, 전체 단어가 다른 전체 단어와 어떻게 관련되어 있는지를 볼 수 있기 때문에 아이디어에 대한 다른 시각을 주셔서 감사합니다. –
@EdwardMordechay 배열을 가져와 변수 variable에 .join ("") 넣을 수 있습니다. –
@EdwardMordechay 그리고 시각화를 위해 Excel을 사용하면 CSV를 사용할 수 있습니다. 이 경우 결과 파일은 results.append (i + ","+ str (match_words/total_words) * 100) + "%")로 표시됩니다. 이후 파일 IO가 필요합니다. –