I am trying to calculate the average distance between string list elements. So the average distance between names below.
testList = ['sarah', 'jon', 'mark', 'jessica', 'sarah', 'sarah', 'liz',
'jon', 'liz', 'sarah', 'jon', 'mark']
So Sarah --> Jon
: [0, 2, 1, 1, 0]
. The average distance between them is 0.8.
Sarah --> Mark
: [1, 1, 1]
. The average distance between them is 1.
I started writing code for this, but got stuck. I am also new to Python and would like to learn how to write this code using basic Python fundamentals like loops, instead of using a library.
def nameDistance(nameOne, NameTwo):
distance = 0
distanceList = []
for name in nameList:
if name == nameOne or nameTwo:
#continue parsing through nameList but increment distance by 1 for each name
#that is not nameOne or nameTwo
#once we get to the other name passed in the function
#append distance to distanceList and set distance back to zero
#continue to do this for the rest of nameList
#take the average of distanceList