Home > Net >  I need to print unique words of the value of list
I need to print unique words of the value of list

Time:09-28

There are over 800 words in my variable list words. I split and sorted by length of words the first 100 words into list_one, and remainder in list_two.

What I got for list_one is..

list_one = ['1', 'a', 'a', 'a', 'a', 'a', 'a', 'it', 'is', 'in', 'of', 'be', 'in', 'of', 'or', 'of', 'be', 'on', 'is', 'so', 'in', 'of', 'he', 'is', 'of', 'or', 'of', 'my', 'mr', 'and', 'set', 'url', 'org', 'txt', 'man', 'the', 'man', 'may', 'his', 'the', 'the', 'the', 'one', 'his', 'jane', '1342', 'that', 'good', 'must', 'want', 'wife', 'such', 'this', 'well', 'that', 'some', 'dear', 'said', 'lady', 'title', 'pride', 'utf-8', 'https', 'files', 'truth', 'known', 'views', 'first', 'truth', 'fixed', 'minds', 'other', 'their', 'author', 'austen', '1342-0', 'single', 'little', 'bennet', 'english', 'chapter', 'fortune', 'however', 'language', 'encoding', 'feelings', 'entering', 'families', 'rightful', 'property', 'prejudice', 'character', 'gutenberg', 'daughters', 'possession', 'considered', 'universally', 'surrounding', 'acknowledged', 'neighbourhood']

Now I have to change the list of list_one to a set. What I did is:

print(set(list_one))

However, it shows me random words outputs of mix of list_one and list_two. How can I fix the problem? I don't understand why I don't get unique words of list_one.

It should look like this:

['a', '1', 'is', 'he', 'be', 'in', 'of', 'or', 'mr', 'my', 'so', 'on', 'it', 'txt', 'set', 'one', 'url', 'and', 'his', 'org', 'man', 'the', 'may', 'jane', 'wife', 'this', '1342', 'want', 'said', 'some', 'that', 'such', 'must', 'lady', 'well', 'good', 'dear', 'pride', 'https', 'known', 'other', 'their', 'title', 'first', 'truth', 'fixed', 'files', 'utf-8', 'minds', 'views', 'little', 'author', 'single', 'bennet', '1342-0', 'austen', 'chapter', 'english', 'however', 'fortune', 'feelings', 'property', 'encoding', 'rightful', 'entering', 'families', 'language', 'prejudice', 'gutenberg', 'daughters', 'character', 'considered', 'possession', 'universally', 'surrounding', 'acknowledged', 'neighbourhood']

CodePudding user response:

set forms unordered collection of unique elements. In order to get ordered collection, sorted function with proper key parameter can be used:

list_one = ['1', 'a', 'a', 'a', 'a', 'a', 'a', 'it', 'is', 'in', 'of', 'be', 'in', 'of', 'or', 'of', 'be', 'on', 'is', 'so', 'in', 'of', 'he', 'is', 'of', 'or', 'of', 'my', 'mr', 'and', 'set', 'url', 'org', 'txt', 'man', 'the', 'man', 'may', 'his', 'the', 'the', 'the', 'one', 'his', 'jane', '1342', 'that', 'good', 'must', 'want', 'wife', 'such', 'this', 'well', 'that', 'some', 'dear', 'said', 'lady', 'title', 'pride', 'utf-8', 'https', 'files', 'truth', 'known', 'views', 'first', 'truth', 'fixed', 'minds', 'other', 'their', 'author', 'austen', '1342-0', 'single', 'little', 'bennet', 'english', 'chapter', 'fortune', 'however', 'language', 'encoding', 'feelings', 'entering', 'families', 'rightful', 'property', 'prejudice', 'character', 'gutenberg', 'daughters', 'possession', 'considered', 'universally', 'surrounding', 'acknowledged', 'neighbourhood']

unique_words = sorted(set(list_one), key=lambda x: len(x))

unique_words should look like:

['a', '1', 'is', 'in', 'so', 'mr', 'on', 'or', 'it', 'of', 'be', 'he', 'my', 'one', 'url', 'org', 'txt', 'the', 'and', 'his', 'man', 'may', 'set', 'well', 'lady', 'wife', 'dear', 'jane', 'that', 'must', 'good', 'said', 'this', 'such', 'some', 'want', '1342', 'files', 'fixed', 'https', 'title', 'known', 'views', 'first', 'other', 'pride', 'minds', 'their', 'truth', 'utf-8', 'bennet', 'single', '1342-0', 'author', 'little', 'austen', 'english', 'chapter', 'fortune', 'however', 'language', 'encoding', 'feelings', 'property', 'rightful', 'entering', 'families', 'character', 'prejudice', 'gutenberg', 'daughters', 'possession', 'considered', 'universally', 'surrounding', 'acknowledged', 'neighbourhood']

CodePudding user response:

Set should return only unique values. Either something is not right with your code and myabe you reassigned "list_one" to some mix of both list or something went wrong with your splitting. Can you share the whole code ?

  • Related