Python | Eliminar todas las palabras duplicadas de una oración dada

Dada una oración que contiene n palabras/strings. Elimine todas las palabras/strings duplicadas que sean similares entre sí.
Ejemplos: 
 

Input : Geeks for Geeks
Output : Geeks for

Input : Python is great and Java is also great
Output : is also Java Python and great

Python

from collections import Counter
  
def remov_duplicates(input):
  
    # split input string separated by space
    input = input.split(" ")
  
    # now create dictionary using counter method
    # which will have strings as key and their 
    # frequencies as value
    UniqW = Counter(input)
  
    # joins two adjacent elements in iterable way
    s = " ".join(UniqW.keys())
    print (s)
  
# Driver program
if __name__ == "__main__":
    input = 'Python is great and Java is also great'
    remov_duplicates(input)

Python

# Program without using any external library
s = "Python is great and Java is also great"
l = s.split()
k = []
for i in l:
  
    # If condition is used to store unique string 
    # in another list 'k' 
    if (s.count(i)>=1 and (i not in k)):
        k.append(i)
print(' '.join(k))

Python3

# Python3 program
  
string = 'Python is great and Java is also great'
  
print(' '.join(dict.fromkeys(string.split())))

Publicación traducida automáticamente

Artículo escrito por AFZAL ANSARI y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *