Python Forum
Numeric Anagrams - Count Occurances
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Numeric Anagrams - Count Occurances
#1
Question 
Greetings,

I am new to Python and messing around with a script with that will read a text file. Each line in the text file contains a three digit number from 000 to 999, the number of lines in the text file can vary. The script I made counts the number of occurrences for each number. However what I want to expand that and get the numeric anagram count.
For example if I have the list as shown below:

123
132
750
213
231
312
321
123
990
570

i would like the output to be:
(123,132,213,231,321,312) = 7
(099,909,990) = 1
(057, 075, 507, 570, 705, 750) = 2


This is the code I have to count the occurrences of a number. I know it can be cleaned up a but I am still just learning and will figure out how to clean it up, but I just need help with the code for the numeric anagram portion. I have searched the forum but only came across word anagram code. Any help would be greatly appreciated.

Regards,
Monty

for occurance in range(000, 100):
    file = open("/home/monty/Documents/Python_Projects/Book1-new.txt", "r")
    data = file.read()
    occurance_fill = f"{occurance:03d}"
    occurances = data.count(str(occurance_fill))

for occurance2 in range(100, 1000):
    file = open("/home/monty/Documents/Python_Projects/Book1-new.txt", "r")
    data = file.read()
    occurance2_fill = f"{occurance2}"
    occurances = data.count(str(occurance2_fill))
    print(str(occurance2_fill) + " : " + str(occurances))

Attached Files

.txt   Book1-new.txt (Size: 4.69 KB / Downloads: 193)
Reply
#2
Although these are numbers, you can still deal with it as strings. If you alphabetically sort each number, you can collapse each to be the "reference" number for each of the anagrams. So "312" would always reference "123".

Then you can just count 'em up. This code doesn't show all the possible anagrams (that is doable with the more-itertools.distinct_permutations). It just shows the reference one.

from collections import Counter

num_list = """123
132
750
213
231
312
321
123
990
570"""

num_frequency = Counter()
for number in num_list.splitlines():
    num_frequency["".join(sorted(number))] += 1

for number, frequency in num_frequency.items():
    print(f"({number}) = {frequency}")
Output:
(123) = 7 (057) = 2 (099) = 1
supuflounder likes this post
Reply
#3
Thank you BowloFred! That helps so much.
I am learning Python on my own so sometimes I just need to be pointed in the right direction.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Numeric Enigma Machine idev 9 485 Mar-29-2024, 06:15 PM
Last Post: idev
  Row Count and coloumn count Yegor123 4 1,321 Oct-18-2022, 03:52 AM
Last Post: Yegor123
  How to get datetime from numeric format field klllmmm 3 1,999 Nov-06-2021, 03:26 PM
Last Post: snippsat
  Extract continuous numeric characters from a string in Python Robotguy 2 2,630 Jan-16-2021, 12:44 AM
Last Post: snippsat
  How to calculate column mean and row skip non numeric and na Mekala 5 4,929 May-06-2020, 10:52 AM
Last Post: anbu23
  Alpha numeric element list search rhubarbpieguy 1 1,778 Apr-01-2020, 12:41 PM
Last Post: pyzyx3qwerty
  convert a character to numeric and back Skaperen 2 2,102 Jan-28-2020, 09:32 PM
Last Post: Skaperen
  are numeric types passed by value or reference? rudihammad 4 2,616 Nov-19-2019, 06:25 AM
Last Post: rudihammad
  'Age' categorical (years -months -days ) to numeric Smiling29 4 2,916 Oct-17-2019, 05:26 PM
Last Post: Smiling29
  how to do a numeric sort Skaperen 11 4,930 Jul-12-2019, 09:50 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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