# Dividing the length of a list

#1

Hi guys

Please can someone help me with this…I’m wanting to make a ‘term frequency’ program which counts the strings in a list and then gives the number of times the strings have been repeated, for example…

terms=[‘a’,‘horse’,‘a’,‘fast’,‘horse’,‘jumps’,‘over’,‘the’,‘smart’,‘duck’]

({‘a’: 2, ‘horse’: 2, ‘over’: 1, ‘fast’: 1, ‘duck’: 1, ‘the’: 1, ‘jumps’: 1, ‘smart’: 1})

However, I then need to then divide the string frequency by the amount of strings in the list…So in this case 10!

This is what I have so far, how do I divide the counts by the length of terms (number of strings in the list) to get termfrequency?

from collections import Counter

terms=[‘a’,‘horse’,‘a’,‘fast’,‘horse’,‘jumps’,‘over’,‘the’,‘smart’,‘duck’]

counts = Counter(terms)

termfrequency = counts/terms

print(counts)

Counter({})

Any replies appreciated

#2

Are you asking how to get the size of a list? Have you tried googling that? “python size of list”

Or are you asking about how to do division? Probably not. But you’re mentioning lots of things and it’s difficult to tell which one is giving you trouble.

If you mean to ask about how to get information out of your Counter object, then googling “python collections module” will get you results near the top telling you all about what you can do with a Counter

…Or if you know all of that and you’re having trouble putting it all together, then show that you’ve found out how to get the required data and then describe what exactly you want to do with it along with what’s stopping you from writing that down as code yourself

#3

My problem is that I need to find how to divide ‘counts’ (which hold the number of times a sting has been repeated) by the length of ‘terms’ which is 10. I know that len(terms) will return 10. I’m new to programming and tried termfrequency = counts/ len(terms) which i thought might work, but it doesn’t. That is what I need help with

#4

Do you? Cause that’s `/` , yeah? So even though that’s your question, what you really want an answer to is something else.

No, it refers to a Counter object. You seem to be saying it refers to an integer, or a float. And which string would “a string” be? All of them? If all, then how would dividing multiple work? If you think about it, this action probably doesn’t make sense.

You probably want to divide each count by the length.

Before even trying this, you would want to check if the Counter object supports that operation. You would also want to read what it promises that this operation does.
Counter doesn’t implement division, so whatever you meant that this should do, you have to write yourself. Again, looking up the docs for Counter would tell you what you can do with it, and then you can consider how you can use those operations to do what you want. https://docs.python.org/library/collections.html

…I think you should grab pen and paper and do the whole thing (computing frequencies) manually and just observe what you do. Then for each thing you did, consider what the matching Python code is. (Or skip the pen and paper, the point is just to think about the operations required to get what you want)

You know how to divide. You know what that length is. So that leaves obtaining each count. I’m sure that counter object supports getting those out somehow, right?

#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.