vinostroud / PDI_Bytes

0 stars 0 forks source link

Bite 4. Top 10PyBites #3

Closed vinostroud closed 1 year ago

vinostroud commented 1 year ago

For Bite 4, this is the first time I've tried to use ElementTree so I am getting a little stuck on getting a proper count. I assume the issue is with how I am assigning categories.

def get_pybites_top_tags(n=5):
    tree = ET.parse(tempfile)
    root = tree.getroot()
    categories = [category.text for category in root.iter('category')]
    category_count = Counter(categories)
    top_tags = category_count.most_common(n)
    print (top_tags)

I can get the right count using BeautifulSoup except PyBites rejects using BS4 :( and I'd need to work on properly formatting the output.

soup = BeautifulSoup(content, 'lxml')

def get_pybites_top_tags(n):
    categories = soup.find_all('category')
    category_count = Counter([category.text for category in categories])
    top_tags = category_count.most_common(n)
    tag_list = [f"({tag}: {count})" for tag, count in top_tags]
    return tag_list

print(get_pybites_top_tags(5))

So I think I have two challenges:

  1. Getting the right count
  2. Formatting the list such that it returns looking like:
               [('python', 79),
                ('learning', 79),
                ('codechallenges', 72),...

Whereas I'm getting:

['(python: 79)', '(learning: 79)', '(codechallenges: 72)',