Python 在两个列表中循环一次一个元素
我有两份相同的清单。我想从列表1中获取第一个元素并将其与列表2中的每个元素进行比较,完成后,我想从列表1中获取第二个元素并重复,直到两个列表中的每个元素都进行了比较 我已经创建了一个Levenshtein距离模型,并且能够成功地在第二个列表中循环1个字符串(我硬编码)。但是,我需要使其更实用,将目标字符串作为列表,并在完成前一个元素与第二个列表的比较后将其切换到下一个元素。然后我只希望它返回大于特定阈值的值,例如80.00Python 在两个列表中循环一次一个元素,python,Python,我有两份相同的清单。我想从列表1中获取第一个元素并将其与列表2中的每个元素进行比较,完成后,我想从列表1中获取第二个元素并重复,直到两个列表中的每个元素都进行了比较 我已经创建了一个Levenshtein距离模型,并且能够成功地在第二个列表中循环1个字符串(我硬编码)。但是,我需要使其更实用,将目标字符串作为列表,并在完成前一个元素与第二个列表的比较后将其切换到下一个元素。然后我只希望它返回大于特定阈值的值,例如80.00 my_list = address['Street'].tolist()
my_list = address['Street'].tolist()
my_list
# Import numpy to perform the matrix algebra necessary to calculate the fuzzy match
import numpy as np
# Define a function that will become the fuzzy match
# I decided to use Levenshtein Distance due to the formulas ability to handle string comparisons of two unique lengths
def string_match(seq1, seq2, ratio_calc = False):
""" levenshtein_ratio_and_distance:
Calculates levenshtein distance between two strings.
If ratio_calc = True, the function computes the
levenshtein distance ratio of similarity between two strings
For all i and j, distance[i,j] will contain the Levenshtein
distance between the first i characters of seq1 and the
first j characters of seq2
"""
# Initialize matrix of zeros
rows = len(seq1)+1
cols = len(seq2)+1
distance = np.zeros((rows,cols),dtype = int)
# Populate matrix of zeros with the indeces of each character of both strings
for i in range(1, rows):
for k in range(1,cols):
distance[i][0] = i
distance[0][k] = k
# loop through the matrix to compute the cost of deletions,insertions and/or substitutions
for col in range(1, cols):
for row in range(1, rows):
if seq1[row-1] == seq2[col-1]:
cost = 0 # If the characters are the same in the two strings in a given position [i,j] then the cost is 0
else:
# In order to align the results with those of the Python Levenshtein package, if we choose to calculate the ratio
# the cost of a substitution is 2. If we calculate just distance, then the cost of a substitution is 1.
if ratio_calc == True:
cost = 2
else:
cost = 1
distance[row][col] = min(distance[row-1][col] + 1, # Cost of deletions
distance[row][col-1] + 1, # Cost of insertions
distance[row-1][col-1] + cost) # Cost of substitutions
if ratio_calc == True:
# Computation of the Levenshtein Distance Ratio
Ratio = round(((len(seq1)+len(seq2)) - distance[row][col]) / (len(seq1)+len(seq2)) * 100, 2)
return Ratio
else:
# print(distance) # Uncomment if you want to see the matrix showing how the algorithm computes the cost of deletions,
# insertions and/or substitutions
# This is the minimum number of edits needed to convert seq1 to seq2
return distance[row][col]
Prev_addrs = my_list
target_addr = "830 Amsterdam ave"
for addr in Prev_addrs:
distance = string_match(target_addr, addr, ratio_calc = True)
print(distance)
忽略了我在你的问题中考虑的所有无关的代码,下面是如何用标题和第一段来完成我认为是问题的本质。
import itertools
from pprint import pprint
def compare(a, b):
print('compare({}, {}) called'.format(a, b))
list1 = list('ABCD')
list2 = list('EFGH')
for a, b in itertools.product(list1, list2):
compare(a, b)
输出:
比较(A,E)调用
比较(A,F)调用
比较(A,G)调用
比较(A,H)调用
比较(B,E)调用
比较(B,F)调用
比较(B,G)调用
比较(B,H)调用
比较(C,E)调用
比较(C,F)调用
比较(C,G)调用
比较(C,H)调用
比较(D,E)调用
比较(D,F)调用
比较(D,G)调用
比较(D,H)调用
因此,您遇到的具体问题是如何构造循环,然后在LD>80时如何保存输出?你在哪里遇到了问题?关于循环,你在寻找类似的东西吗?比较列表1和列表2的所有组合听起来像是@bart cubrich的工作,这是完全正确的。我不确定如何构造循环,并使其适合我的函数。我将研究itertools.product。这看起来是开始解决问题的可靠方法我只是提供了额外的代码作为解决问题的背景,但这是我问题的核心。我真正的问题是如何通过我的函数获得这些迭代对(这就是为什么我也提供了我的函数)。谢谢你解决了我的核心问题!我的答案更新是否解决了“真正”的问题?如果是的话,请考虑接受。看,是的。谢谢你的帮助,马蒂诺!