Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Removal of duplicates
#1
Hi everyone,

I have made an anagram below from a words.txt file.

with open('words.txt', 'r') as read:
    line = read.readlines()

def make_anagram_dict(line):
    word_list = {}

    for word in line:
        word = word.lower()
        key = ''.join(sorted(word))
        if key in word_list and len(word) > 5 and word not in word_list:
            word_list[key].append(word)
        else:
            word_list[key] = [word]

    return word_list

if __name__ == '__main__':
    word_list = make_anagram_dict(line)

    for key, words in word_list.items():
        if len(words) >:
            print('Key value' + ' '*len(key) + '|     words')
            print(key + ' '*len(key) + ':' + str(words))
            print('---------------------------------------------')
The output I get looks like this (on a random part)

Output:
Key value | words hortwy :['worthy\n', 'wrothy\n'] ---------------------------------------------
the problem is that in the words.txt file, It coins duplicates except for the capital letter at the start:
i.e Zipper and zipper. It therefore creates an anagram of zipper, when it shouldn't. I tried to fix it with the part in bold. I would really appreciate any help!
buran write Feb-01-2021, 12:31 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
One way to eliminate duplicates would be to convert to a set, then convert back again.
1. Read the words into a list.
2. Convert the items to lower case.
3. Copy the list to a set
4. Copy the set back to a list
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  remove partial duplicates from csv ledgreve 0 746 Dec-12-2022, 04:21 PM
Last Post: ledgreve
  Removal of items in .txt using python nanakochan 8 1,664 Sep-02-2022, 04:58 PM
Last Post: perfringo
  Problem : Count the number of Duplicates NeedHelpPython 3 4,284 Dec-16-2021, 06:53 AM
Last Post: Gribouillis
  Displaying duplicates in dictionary lokesh 2 1,936 Oct-15-2020, 08:07 AM
Last Post: DeaD_EyE
  how do i pass duplicates in my range iterator? pseudo 3 2,297 Dec-18-2019, 03:01 PM
Last Post: ichabod801
  Deleting duplicates in tuples Den 2 2,717 Dec-14-2019, 10:32 PM
Last Post: ichabod801
  Vertical Seam Removal scott14 0 1,889 Dec-27-2018, 03:03 AM
Last Post: scott14

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020