CAVS CAVS - 9 days ago 8
Python Question

word count in Python

def words(word,number):
if number<len(word):
result={}
for key,value in word.items():
common_num=sorted(set(word.values()), reverse=True)[:number]
if value in common_num:
result.update({key:value})
word.clear()
word.update(result)
new_word_count={}
common_word=[]
common=[]
for key, value in word.items():
if value in common_word:
common.append(value)
common_word.append(value)
new_word_count=dict(word)
for key,value in new_word_count.items():
if value in common:
del word[key]


Example:

>>> word={'a': 2, 'b': 2, 'c' : 3, 'd: 3, 'e': 4, 'f' : 4, 'g' : 5}
>>> words(word,3)


My output: {'g': 5}

Expected output:{'g': 5, 'e': 4, 'f': 4}

Any idea why im getting this output

Answer

Well, without any special imports, there are easier ways to accomplish what you're trying to do. You've got a whole lot of rigmarole involved in tracking and storing the values being kept, then deleting, then re-adding, when you could simplify a lot; even with explanatory comments, this is substantially shorter:

def common_words(word_count, number):
    # Early out when no filtering needed
    if number >= len(word_count):
        return

    # Get the top number+1 keys based on their values
    top = sorted(word_count, key=word_count.get, reverse=True)[:number+1]

    # We kept one more than we needed to figure out what the tie would be
    tievalue = word_count[top.pop()]

    # If there is a tie, we keep popping until the tied values are gone
    while top and tievalue == word_count[top[-1]]:
        top.pop()

    # top is now the keys we should retain, easy to compute keys to delete
    todelete = word_count.keys() - top
    for key in todelete:
        del word_count[key]

There are slightly better ways to do this that avoid repeated lookups in word_count (sorting items, not keys, etc.), but this is easier to understand IMO, and the extra lookups in word_count are bounded and linear, so it's not a big deal.