forked from kushaldas/pym
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
576 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?xml version='1.0'?> | ||
<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" [ | ||
]> | ||
|
||
<chapter id="pythonforyouandme-Collections"> | ||
<title>Collections module</title> | ||
<para> | ||
In this chapter we will learn about a module called <emphasis>Collections</emphasis>. In this module we some nice data structures which will help you to solve various real life problems. | ||
</para> | ||
<programlisting language="Python"> | ||
<![CDATA[>>> import collections]]> | ||
</programlisting> | ||
<para> | ||
This is how you can import the module, now we will see the available classes which you can use. | ||
</para> | ||
<section id="pythonforyouandme-Collections-Counter"> | ||
<title>Counter</title> | ||
<para> | ||
<emphasis>Counter</emphasis> is a <emphasis>dict</emphasis> subclass which helps to count hashable objects. Inside it elements are stored as dictionary keys and counts are stored as values which can be zero or negative. | ||
</para> | ||
<para> | ||
Below we will see one example where we will find occurrences of words in the Python LICENSE file. | ||
</para> | ||
<example> | ||
<title>Counter example</title> | ||
<programlisting language="Python"><![CDATA[>>> from collections import Counter | ||
>>> import re | ||
>>> path = '/usr/share/doc/python-2.7.3/LICENSE' | ||
>>> words = re.findall('\w+', open(path).read().lower()) | ||
>>> Counter(words).most_common(10) | ||
[('2', 97), ('the', 80), ('or', 78), ('1', 76), ('of', 61), ('to', 50), ('and', 47), ('python', 46), ('psf', 44), ('in', 38)]]]> | ||
</programlisting> | ||
</example> | ||
<para> | ||
Counter objects has an method called <emphasis>elements</emphasis> which returns an iterator over elements repeating each as many times as its count. Elements are returned in arbitrary order. | ||
</para> | ||
<programlisting language="Python"> | ||
<![CDATA[>>> c = Counter(a=4, b=2, c=0, d=-2) | ||
>>> list(c.elements()) | ||
['a', 'a', 'a', 'a', 'b', 'b']]]> | ||
</programlisting> | ||
|
||
<para> | ||
<emphasis>most_common</emphasis> is a method which returns most common elements abd their counts from the most common to the least. | ||
</para> | ||
<programlisting language="Python"> | ||
<![CDATA[>>> Counter('abracadabra').most_common(3) | ||
[('a', 5), ('r', 2), ('b', 2)]]]> | ||
</programlisting> | ||
</section> | ||
<section id="pythonforyouandme-Collections-defaultdict"> | ||
<title>defaultdict</title> | ||
<para> | ||
<emphasis>defaultdict</emphasis> is a dictionary like object which provides all methods provided by dictionary but takes first argument (default_factory) as default data type for the dictionary. Using defaultdict is faster than doing the same using <emphasis>dict.set_default</emphasis> method. | ||
</para> | ||
<example> | ||
<title>defaultdict example</title> | ||
<programlisting language="Python"> | ||
<![CDATA[>>> s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)] | ||
>>> d = defaultdict(list) | ||
>>> for k, v in s: | ||
... d[k].append(v) | ||
... | ||
>>> d.items() | ||
[('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]]]> | ||
</programlisting> | ||
</example> | ||
<para> | ||
In the example you can see even if the key is not there in the defaultdict object, it automatically creates an empty list. <emphasis>list.append</emphasis> then helps to append the value to the list. | ||
</para> | ||
</section> | ||
<section id="pythonforyouandme-Collections-namedtuple"> | ||
<title>namedtuple</title> | ||
<para> | ||
Named tuples helps to have meaning of each position in a tuple and allow us to code with better readability and self-documenting code. You can use them in any place where you are using <emphasis>tuples</emphasis>. In the example we will create a namedtuple to show hold information for points. | ||
</para> | ||
<example> | ||
<title>Named tuple</title> | ||
<programlisting language="Python"> | ||
<![CDATA[>>> from collections import namedtuple | ||
>>> Point = namedtuple('Point', ['x', 'y']) #Defining the namedtuple | ||
>>> p = Point(10, y=20) #Creating an object | ||
>>> p | ||
Point(x=10, y=20) | ||
>>> p.x + p.y | ||
30 | ||
>>> p[0] + p[1] #Accessing the values in normal way | ||
30 | ||
>>> x, y = p #Unpacking the tuple | ||
>>> x | ||
10 | ||
>>> y | ||
20]]> | ||
</programlisting> | ||
</example> | ||
</section> | ||
|
||
</chapter> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.