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

Time:09-11

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

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?

CodePudding user response:

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        |
  • Related