Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Coding in Python

Name: Anonymous 2011-09-18 20:34

Hi guys.

It's clear that I don't know shit about programming, so let's get that out of the way.

I'm trying to define a python (2.x) function that'll loop through a number of texts, get the relative frequency of a certain word in each of those texts (number of times the word is used / total words * 100), and then sort the frequencies and tell me which text has the highest one and what the frequency for that text is.

I made a list with the texts as elements, and then I used a for-loop to go through the list. Getting and sorting the frequencies isn't hard if I stick them in a list, but I can't figure out how to keep the right texts associated with the sorted frequencies. If I make a list with pairs like '0.071203, <Text: Moby Dick by Herman Melville 1851>' , I can't sort the frequencies. If I make a list with just the frequencies, I don't know which texts they belong to.

Is there a simple way to do this?

Name: Anonymous 2011-09-18 20:58

>>1
You're a moron, but I'll help you anyway because >>2-5 are huge cocksucking fags.

Keep your stuff in a dict where the key is (title, word) and the value is frequency. Then use filter(lambda x:x[0][0]==a_title, the_dict.iteritems()) or filter(lambda x:x[0][1]==a_word, the_dict.iteritems()) to filter out and retrieve a list of entries that correspond. If you want to sort the resulting list, I suggest using something like the_resulting_list.sort(key=lambda a:a[1]). Hope that helps.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List