Pages

Apr 22, 2015

[Python] A simple way to sort a dictionary

[Python] An example of scraping date from a website and writing to a csv file

In order to scrape data from a website, I used the "BeautifulSoup" module for Python. The data I want to get from the website (http://www.charitynavigator.org/index.cfm?bay=topten.detail&listid=24#.VTfpxa3BzGc) is the "10 Super-Sized Charities."



A sample code is shown below:
And the result is

Apr 16, 2015

[Python] defaultdict will not always generate default value

If you didn't assign default data type, then the defaultdict will not generate a default value for a missing key. For this case, we can use get(key) to find the key value and it will not cause error if the key is missing. See the following example.

Apr 12, 2015

int is not an object in Java

Type int in Java is not an object. So when we assign a previously created int variable to another newly created int variable, they are not sharing the same memory (they are mutually independent).

Unlike type int, String is an object in Java. When we assign a previously created string assigned to another newly created string, they share the same object.
PS. String is not mutable.

Apr 4, 2015

[Python] Counter

The function of "Counter" make it very convenient to count the number of repeated items in two list. See the following example.

Apr 3, 2015

[Python] some operations on list

When using a list in Python, we can see the indicator of the items as follows:
x = [0, 1, 2, 3, 4, 5, 6, 7, 8]
x[0],   x[1],   x[2],   x[3],   x[4],   x[5],   x[6],   x[7],   x[8]
x[-0],  x[-8], x[-7],  x[-6],  x[-5], x[-4],  x[-3],  x[-2], x[-1]   <= reverse direction
x[-9]
some usages of list in Python are shown below: