Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yup, that's a whole lot of crap you either don't need or shouldn't do. Here's how I'd do it prior to Python 2.7 (after that, use collections.Counter as shown in the other answers). Mind you, this'll return the dictionary containing the counts, not print it, you'd have to do that externally. I'd also not prefer to give a complete solution for homeworks, but it's already been done, so I suppose there's no real damage in explaining a bit about it. </p> <pre><code>def parseFile(filename): with open(filename, 'r') as fh: lines = fh.readlines() d={} for country in [line.split(',')[1].strip() for line in lines]: d[country] = d.get(country,0) + 1 return d </code></pre> <p>Lets break that down a bit, shall we? </p> <pre><code> with open(filename, 'r') as fh: lines = fh.readlines() </code></pre> <p>This is how you'd normally open a text file for reading. It will raise an IOError exception if the file doesn't exist or you don't have permissions or the likes, so you'll want to catch that. readlines() reads the entire file and splits it into lines, each line becomes an element in a list.</p> <pre><code> d={} </code></pre> <p>This simply initializes an empty dictionary</p> <pre><code> for country in [line.split(',')[1].strip() for line in lines]: </code></pre> <p>Here is where the fun starts. The bracket enclosed part to the right is called a list comprehension, and it basically generates a list for you. What it pretty much says, in plain english, is <em>"for each element 'line' in the list 'lines', take that element/line, split it on each comma, take the second element (index 1) of the list you get from the split, strip off any whitespace from it, and use the result as an element in the new list"</em> Then, the left part of it just iterates over the generated list, giving the name 'country' to the current element in the scope of the loop body. </p> <pre><code> d[country] = d.get(country,0) + 1 </code></pre> <p>Ok, ponder for a second what would happen if instead of the above line, we'd used the following:</p> <pre><code> d[country] = d[country] + 1 </code></pre> <p>It'd crash, right (KeyError exception), because d[country] doesn't have a value the first time around. So we use the get() method, all dictionaries have it. Here's the nifty part - get() takes an optional second argument, which is what we want to get from it if the element we're looking for doesn't exist. So instead of crashing, it returns 0, which (unlike None) we can add 1 to, and update the dictionary with the new count. Then we just return the lot of it. </p> <p>Hope it helps. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload