i've looked up a few threads on here but not one actually matches my situation.
I have basically a text file that looks something like this:
orange 0 0 0
orange 1 0 0
orange 2 0 0
orange 3 0 0
orange 4 0 0
orange 5 0 0
apple 0 0 0
apple 1 0 0
apple 2 0 0
apple 3 0 0
apple 4 0 0
apple 5 0 0
grapes 0 0 0
grapes 1 0 0
grapes 2 0 0
grapes 3 0 0
grapes 4 0 0
grapes 5 0 0
what I need to do, is to be able to take the first word as a string, and search how many lines contain that first word, then move on to the next word, and search for how many lines that contains that word. So the result should look something like this:
firstTermCount: 6
secondTermCount: 6
thirdTermCount: 6
I need have this count number, so that in the next step I can have a command that is supposed to run in the range of exactly how many lines of that string occurs to utilize the numbers next to each word.
the issue here is that, I have no idea what those terms are actually going to be called, so I can't do this whole "Count" or "count_dict" technique i keep seeing, since to me it seems like you need to have a set name for the function to actually look for. Plus I have no idea how many lines there will be in a file each time, I would have to do it each time I read a file. I know the example i wrote had five lines each, but honestly the type of file I want to read will have a random number of lines so I can't just say like "look for it 5 times"
Could anyone provide a solution to this issue, or perhaps a link to a thread that answers this question that I may have missed...?
Thank you
Note: I am using Python v2.6.4, if that helps
EDIT So a user suggested that I use the Counter feature, or use this dictionary method, but either way it doesn't quite give me the result I need. So for example, using this Counter method (i used a work around listed here:
new list:
orange 0 0 0
orange 1 0 0
orange 2 0 0
orange 3 0 0
orange 4 0 0
apple 1 0 0
apple 2 0 0
apple 4 0 0
apple 5 0 0
grapes 1 0 0
grapes 2 0 0
grapes 4 0 0
peaches 0 0 0
peaches 1 0 0
peaches 2 0 0
peaches 3 0 0
peaches 5 0 0
peaches 6 0 0
and this is what the counter method gives me:
{'orange': 5, 'peaches': 6, 'apple': 4, 'grapes': 3}
when what I WANT is this:
{'orange': 5, 'apple': 4, 'grapes': 3,'peaches': 6 }
How can i get these counts in this order?