-1

I am trying to compare 2 street networks and when i run this code it returns a a ratio of .253529... i need it to compare each row to get a unique value so i can query out the streets that dont match. What can i do it get it to return unique ratio values per row?

# Set local variables
inFeatures = gp.GetParameterAsText(0)
fieldName = gp.GetParameterAsText(1)
fieldName1 = gp.GetParameterAsText(2)
fieldName2 = gp.GetParameterAsText(3)
expression = difflib.SequenceMatcher(None,fieldName1,fieldName2).ratio()

# Execute CalculateField arcpy.CalculateField_management(inFeatures, fieldName, expression, "PYTHON_9.3")

  • What is your question? what is gp? – user823743 Dec 18 '14 at 16:02
  • What do you mean by "unique value"? Give us an example of your desired output – wnnmaw Dec 18 '14 at 16:05
  • I want it to give me a index value of the similarities between the two street names. for example if it is fieldname1 is Broad Street and fieldname 2 is Broad Street then the output would be 1.whereas if the fieldname1 was Braod Sreet and fieldname 2 was Broad St the outcome would be lower than 1. – Stephen Holt Dec 18 '14 at 16:16
  • @StephenHolt, thats exactly what ```ration()``` will give you – wnnmaw Dec 18 '14 at 17:52
  • @wnnmaw it is giving me a ratio but i need it to run for all of the streets not just one. the fieldnames have thousand of streets so i need it to give me an index value based for each specific row it is calcualting – Stephen Holt Dec 18 '14 at 18:16
  • @StephenHolt, so you want to compute the ratio for each line of the files? – wnnmaw Dec 18 '14 at 18:30

1 Answers1

0

If you know both files always have the exact same number of lines, a simple approach like this would work:

ratios = []

with open('fieldName1', 'r') as f1, open('fieldName2', 'r') as f2:
    for l1, l2 in zip(f1, f2):
        R = difflib.SequenceMatcher(None,l1,l2).ratio()
        ratios.append((l1, l2, R))

This will produce a list of tuples like this:

[("aa", "aa", 1), ("aa", "ab", 0.5), ...]

If your files are different sizes you'll need to find some way to match up the lines, or otherwise handle it

wnnmaw
  • 5,444
  • 3
  • 38
  • 63