Home > OS >  Detecting Unique Terms In A List Contained of Multiple Strings
Detecting Unique Terms In A List Contained of Multiple Strings

Time:12-09

example = ["duran duran sang wild boys in 1984", "wild boys don't remain forever wild", "who brought wild flowers","it was john krakauer who wrote in to the wild"]

How do I detect unique terms and put them in a list like this:

['duran', 'sang', 'wild', 'boys', 'in', '1984', "don't", 'remain', 'forever', 'who', 'brought', 'flowers', 'it', 'was', 'john', 
'krakauer', 'wrote', 'to', 'the']

My code:

def uniqueterms(a, d, e, f) :    
    b = a.split()
    c = [] `

    for x in b:
        if a.count(x) >= 1 and (x not in c):
            c.append(x)
    print((' '.join(c)).split(), end=' ')
    g = d.split()
    h = []

    for y in g:
        if d.count(y) >= 1 and (y not in h):
            h.append(y)
    print((' '.join(h)).split(), end=' ')
    i = e.split()
    j = []

    for z in i:
        if e.count(z) >= 1 and (z not in j):
            j.append(z)
    print((' '.join(j)).split(), end=' ')
    k = f.split()
    m = []

    for t in k:
        if f.count(t) >= 1 and (t not in m):
            m.append(t)
    print((' '.join(m)).split())

>>> uniqueterms(example[0], example[1], example[2], example[3])
['duran', 'sang', 'wild', 'boys', 'in', '1984'] ['wild', 'boys', "don't", 'remain', 'forever'] ['who', 'brought', 'wild', 'flowers'] ['it', 'was', 'john', 'krakauer', 'who', 'wrote', 'in', 'to', 'the', 'wild']

CodePudding user response:

*Updated to return unique words in order of their appearance. The previous version using python set() was not sensitive to input order:

def get_unique_words(text):
    visited = set()
    uniq = []
    for word in text.split():
        if word not in visited:
            uniq.append(word)
            visited.add(word)
    return uniq

To handle a list of strings:

def get_unique_words_from_list_of_strings(str_list):
    return get_unique_words(' '.join(str_list))

To run your example:

words_in_order = get_unique_words_from_list_of_strings(example)

which returns

['duran', 'sang', 'wild', 'boys', 'in', '1984', "don't", 'remain', 'forever', 'who', 'brought', 'flowers', 'it', 'was', 'john', 'krakauer', 'wrote', 'to', 'the']
  • Related