Python | Learn for Master - Part 3
  • Learn Python List by Examples

    Python List is a powerful data structure that you almost need it in every program. Python List can be used to hold a collection of items which can  be any type. In this post, I will use examples to describe how to use python List. 

    Python List Declaration

    It is easy to create a python list object. The following code declare a List L and initialize it with 4 values.

    In [1]:

    L = [1, 2, 'Jim', ('hello, word')]
    print L


    [1, 2, 'Jim', 'hello, word']


    Data look up in python List

    We can locate the elements in the List by index.

    [Read More...]
  • LeetCode – Next Permutation (Python)

    Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.

    If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order). The replacement must be in-place, do not allocate extra memory.

    Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.

    The solution is the Pandit’s algorithm

    1 Pandit’s algorithm
    The article in Wikipedia describes the algorithm invented by Narayana Pandit to changes the list in-place to generate the next permutation given a list or Array A.

    [Read More...]
  • Python Dictionary Example

    Python dict structure is similiar to Java’s HashMap or HashTable.
    It aims to store key value pairs.

    Here is an example:
    we build a dict to map a username to his profile information, which is a tuple

    username => (Full_name, gender, age)

    Search in Dict

    Now we can do a look up by username with O(1) time complexity.
    Search the user with username equal to ‘jim’

    Key not exist exception

    you may get key not exist exception when the username is not in the table

    Do key check

    We can get around this issue by an exist check

    Use get with a default value

    A better method is to use the get() method with default value.

    [Read More...]
Page 3 of 3123