1

Possible Duplicate:
How to check if my list has an item from another list(dictionary)?

This is actually homework for a mark.

The user of program must write sentence down. Than program checks the words and prints the wrong ones (if wrong words appear more than once program must print them only once). Wrong words must be printed in the order they appear in the sentence.

Here is how I did it. But there is one problem. The wrong words do not apper in the same order they apper in the sentence beacause of built-in function sorted. Is there any other method to delete duplicates in list?

And dictionary is imported from dictionary.txt!!

sentence=input("Sentence:")
dictionary=open("dictionary.txt", encoding="latin2").read().lower().split()
import re
words=re.findall("\w+",sentence.lower()) 

words=sorted(set(words)) 
sez=[]
for i in words:
     if i not in dictionary:
         sez.append(i)

print(sez)
Community
  • 1
  • 1
Miki
  • 65
  • 1
  • 2
  • 7

4 Answers4

2
words = filter(lambda index, item: words.index(item) == index, enumerate(words))

It'll filter out every duplicate and will maintain the order.

As Thomas pointed out, this is a rather heavy approach. if you need to process a larger number of words, you could use this for loop:

dups = set()
filtered_list = []
for word in words:
    if not word in dups:
        filtered_list.append(word)
        dups.add(word)
unddoch
  • 5,790
  • 1
  • 24
  • 37
1

To delete duplicates in a list, add them to a dictionary. A dictionary only has 1 KEY:VALUE pair.

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
0

You can use OrderedSet recipe.

@edit: BTW if the dictionary is big then it's better to convert dictionary list into a set -- checking existence of an element in a set takes constant time instead of O(n) in the case of list.

Chris Medrela
  • 586
  • 3
  • 8
0

You should check this answer:

https://stackoverflow.com/a/7961425/1225541

If you use his method and stop sorting the words array (remove the words=sorted(set(words)) line) it should do what you expect.

Community
  • 1
  • 1
alestanis
  • 21,519
  • 4
  • 48
  • 67