Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can you print a decorated frequency table given two lists?

I am trying to create a class that can act as a frequency table for a calculator project. I am having issues with printing the table in a nicely formatted way. Here is my current code:

class FrequencyTable:
    def __init__(self, data: list, freq: list):
        self.data = data
        self.freq = freq
        self.table_as_dict = {}
        for item in self.data:
            item = str(item)
            self.table_as_dict[item] = self.freq

    def print(self):
        print("|Item|Frequency|")
        print("|--------------|")
        for item in self.table_as_dict:
            item = str(item)
            while True:
                if f"{str(item)}" < 4:
                    item = f"{item} "
                else:
                    break
            r_item = self.table_as_dict[item]
            while True:
                if len(item) < 9:
                    r_item = f"{r_item} "
                else:
                    break
            print(f"|{item}|{r_item}|")

The print function works until the first if statement in the while loop, which throws the exception

TypeError: object of type ‘int’ has no len() e.g

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

table = FrequencyTable([1,2,3], [7,8,9])

table.table_as_dict would be {1: 7, 2: 8, 3: 9}. If i called print, I would get:

|Item|Frequency|
|--------------|
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "filepath", line 14, in print
    item = str(item)

How can I get the print function to print all of the data whilst keeping it’s formatting?

>Solution :

I believe what you are trying to do in the while loops can be achieved with ljust on string objects.

class FrequencyTable:
    def __init__(self, data: list, freq: list):
        self.data = data
        self.freq = freq
        self.table_as_dict = {}
        for item in self.data:
            item = str(item)
            self.table_as_dict[item] = self.freq

    def print(self):
        item_ljust = 10
        freq_ljust = 9
        print(f"|{'Item'.ljust(item_ljust)}|{'Frequency'.ljust(freq_ljust)}|")
        print(f"|{''.join('-' for _ in range(item_ljust+freq_ljust+1))}|")
        for item in self.table_as_dict:
            item = str(item)
            r_item = self.table_as_dict[item]
            print(f"|{item.ljust(item_ljust)}|{str(r_item).ljust(freq_ljust)}|")


d = FrequencyTable(data=["1", "2", "3"], freq=[3, 4, 5])
d.print()

output:

|Item      |Frequency|
|--------------------|
|1         |[3, 4, 5]|
|2         |[3, 4, 5]|
|3         |[3, 4, 5]|

However your table dictionary may have been wrong and what you really wanted was:

class FrequencyTable:
    def __init__(self, data: list, freq: list):
        self.data = data
        self.freq = freq
        self.table_as_dict = {k: v for k, v in zip(data, freq)}

    def print(self):
        item_ljust = 10
        freq_ljust = 9
        print(f"|{'Item'.ljust(item_ljust)}|{'Frequency'.ljust(freq_ljust)}|")
        print(f"|{''.join('-' for _ in range(item_ljust+freq_ljust+1))}|")
        for item, r_item in self.table_as_dict.items():
            print(f"|{item.ljust(item_ljust)}|{str(r_item).ljust(freq_ljust)}|")


d = FrequencyTable(data=["1", "2", "3"], freq=[3, 4, 5])
d.print()

Output:

|Item      |Frequency|
|--------------------|
|1         |3        |
|2         |4        |
|3         |5        |
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading