Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4241982
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:22:54+00:00 2026-05-21T03:22:54+00:00

first i want to say that i am a newbie in python. I trying

  • 0

first i want to say that i am a newbie in python. I trying to calculate the Levenshtein Distance for many lists of word. Until now i succeed writing the code for a pair of word, but i’m having some problems doing it for lists. I just habe two lists with words one below the other like this:
carlos
stiv
peter

I want to use the Levenshtein distance for a similarity approach. Could somebady tell me how i can load the lists and then use a function to calculate de distance?

I’ll appreciated!

Here is my code just for two strings:

#!/usr/bin/env python
# -*- coding=utf-8 -*-

def lev_dist(source, target):
    if source == target:
        return 0

#words = open(test_file.txt,'r').read().split();

    # Prepare matrix
    slen, tlen = len(source), len(target)
    dist = [[0 for i in range(tlen+1)] for x in range(slen+1)]
    for i in xrange(slen+1):
        dist[i][0] = i
    for j in xrange(tlen+1):
        dist[0][j] = j

    # Counting distance
    for i in xrange(slen):
        for j in xrange(tlen):
            cost = 0 if source[i] == target[j] else 1
            dist[i+1][j+1] = min(
                            dist[i][j+1] + 1,   # deletion
                            dist[i+1][j] + 1,   # insertion
                            dist[i][j] + cost   # substitution
                        )
    return dist[-1][-1]

if __name__ == '__main__':
    import sys
    if len(sys.argv) != 3:
        print 'Usage: You have to enter a source_word and a target_word'
        sys.exit(-1)
    source, target = sys.argv[1], sys.argv[2]
    print lev_dist(source, target)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T03:22:54+00:00Added an answer on May 21, 2026 at 3:22 am

    I finally got the code working with some help from a friend 🙂
    You can compute the Levenshtein distance and compare it to every word from the second list changing the last line in the script, i.e: print(list1[0], list2[i]), to compare the first word from the list1 to every word in list2.

    Thanks

    #!/usr/bin/env python
    # -*- coding=utf-8 -*-
    
    import codecs
    
    def lev_dist(source, target):
        if source == target:
            return 0
    
    
        # Prepare a matrix
        slen, tlen = len(source), len(target)
        dist = [[0 for i in range(tlen+1)] for x in range(slen+1)]
        for i in range(slen+1):
            dist[i][0] = i
        for j in range(tlen+1):
            dist[0][j] = j
    
        # Counting distance, here is my function
        for i in range(slen):
            for j in range(tlen):
                cost = 0 if source[i] == target[j] else 1
                dist[i+1][j+1] = min(
                                dist[i][j+1] + 1,   # deletion
                                dist[i+1][j] + 1,   # insertion
                                dist[i][j] + cost   # substitution
                            )
        return dist[-1][-1]
    
    # load words from a file into a list
    def loadWords(file):
        list = [] # create an empty list to hold the file contents
        file_contents = codecs.open(file, "r", "utf-8") # open the file
        for line in file_contents: # loop over the lines in the file
            line = line.strip() # strip the line breaks and any extra spaces
            list.append(line) # append the word to the list
        return list
    
    if __name__ == '__main__':
        import sys
        if len(sys.argv) != 3:
            print 'Usage: You have to enter a source_word and a target_word'
            sys.exit(-1)
        source, target = sys.argv[1], sys.argv[2]
    
        # create two lists, one of each file by calling the loadWords() function on the file
        list1 = loadWords(source)
        list2 = loadWords(target)
    
        # now you have two lists; each file has to have the words you are comparing on the same lines
        # now call you lev_distance function on each pair from those lists
    
        for i in range(0, len(list1)): # so now you are looping over a range of numbers, not lines
            print lev_dist(list1[0], list2[i])
    
    
    #    print lev_dist(source, target)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say that I have a two word string and I want to capitalize
I have a an Android ListView that has small (say, 1-5 frame) stutters as
I just finished my first own example with activities/places and MVP. all works fine
I have a directed graph described by A -> B meaning that there exists
I'm trying to get a list of link titles from a wikipedia page. I
this my first question on here. I hope someone can help. It is to
I need to track changes created in a directory and also saved the history.
I read a lot of question and answer on TDD and unit testing on
I am creating a time machine with c#. A time machine is a way
My main form has two panels, left docked and right docked. The right side

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.