Python Tutorial

Showing posts with label List. Show all posts
Showing posts with label List. Show all posts

Thursday, January 16, 2014

python filter(), map(), reduce() with list

  • filter(function, sequence) : returns sequence for which function(item) is true
  • map(function, sequence): call function(item) for each item and returns a list
  • reduce(function, sequence): returns a single value constructed by calling function on first two items on the sequence, then the result and the next item, and so on.
def f(x):
    return x % 2 == 0
print filter(f, range(1,10))


def square(x):
    return x*x
print map(square, range(1,5))

def add(x, y):
    return x+y
print reduce(add, range(1,10))

Output:
[2, 4, 6, 8]
[1, 4, 9, 16]
45

Friday, December 20, 2013

Python list comprehensions

Python list comprehensions provide many way to create list.
The comrehensions format is [*transform* *iteration* *filter* ].
Let's have some example.

# read a file line by line check whether or not "word" contains in each line (ignore case)
print [ line.strip("\n").lower() for line in open("a.in") if "word" in line]

# initialize array by square number
print [i**2 for i in range(10)]
# initialize array with tuples 
print [(i,i**2) for i in range(10)]

# initialize array by odd number
print [i for i in range(10) if i%2]

words = ["Life", "is", "very", "easy", "Python"]
# print first character of each word
print [word[0] for word in words]

# make all words to upper case
print [word.upper() for word in words]

# Sum each element of two array
a = [1, 2, 3]
b = [4, 5, 6]
print [a[i]+b[i] for i in range(len(a))]

Thursday, August 26, 2010

python stack data structure


Stack is very important data structure. Here a simple stack implementation.
All source code available on github

 #DA_Stack.py

class MyStack():
    stack = []
    max_stack_size = 2

    def push(self, value):
        if len(self.stack) == self.max_stack_size:
            return "Overflow"
        self.stack.append(value)
    def pop(self):
        if(len(self.stack)):
            return self.stack.pop()
        return "Underflow"

myStack = MyStack()
myStack.push(2)
myStack.push(5)
print myStack.stack
print myStack.push(6)
print myStack.stack
print myStack.pop()
print myStack.stack
print myStack.pop()
print myStack.pop()
print myStack.stack



Output:
[2, 5]
Overflow
[2, 5]
5
[2]
2
Underflow
[]

python queue data structure


Queue implementation using collections.deque.
All source code available on github

#DA_Queue.py

from collections import deque
class MyQueue:
    queue = deque()

    def enqueue(self, value):
        self.queue.append(value)
    def dequeue(self):
        if len(self.queue):
            return self.queue.popleft()
        print "Impossible"


myQueue = MyQueue()
myQueue.enqueue(8)
myQueue.enqueue(4)
print myQueue.queue
myQueue.dequeue()
myQueue.dequeue()
print myQueue.queue
myQueue.dequeue()
print myQueue.queue


Output:
deque([8, 4])
deque([])
Impossible
deque([])

Python list

'''
   This Code use python list syntax
'''

myList=list()

myList.append(1)
myList.append(5)
myList.append(1)
myList.append(2)
myList.append(3)

'''
   Access list data
'''
for i in range(0,len(myList)):
    print myList[i]

'''
   Counting occurance of a data/object
'''
print "myList.count(1) ",myList.count(1)

'''
   Remove a list object
   - remove only first occured value
'''
myList.remove(1)
print myList

myList.append(1)

'''
   Reverse objects of list
'''
print myList
myList.reverse()
print myList

'''
   Find a data/object to list
   - return the lowest index of object
   - raise exception if data not found
'''
print "myList.index(5): ",myList.index(5)

'''
   Sort objects of list
'''

myList.sort()
print myList

'''
   Insert a data/object into list
'''
myList.insert(2,100)
print myList






Output:
1
5
1
2
3
myList.count(1)  2
[5, 1, 2, 3]
[5, 1, 2, 3, 1]
[1, 3, 2, 1, 5]
myList.index(5):  4
[1, 1, 2, 3, 5]
[1, 1, 100, 2, 3, 5]