http://stackoverflow.com/questions/2548000/sorting-a-dictionary-having-keys-as-string-of-numbers-in-python
Monday, June 13, 2016
converting dictionary to ordereddictionary (ordered dictionary)
http://stackoverflow.com/questions/15711755/converting-dict-to-ordereddict
Note: OrderedDict Will not Sort - it will just create the tuples correctly
Note: OrderedDict Will not Sort - it will just create the tuples correctly
Saturday, June 11, 2016
python for loop and range: variable on loop line is not reinitialized
for y in range(x, 10):
... print y
... x += 1
... print y
... x += 1
In the above example the range does not change even if value of x is changing
but the range will change in the example below
for x in range(0, 4):
for y in range(4, x, -1):
print y
python recursive function
http://stackoverflow.com/questions/479343/how-can-i-build-a-recursive-function-in-python
def a(n):
if n == 0: return 0
else: return n + a(n)
def a(n):
if n == 0: return 0
else: return n + a(n)
The two key elements of a recursive algorithm are:
- The termination condition:
n == 0
- The reduction step where the function calls itself with a smaller number each time:
factorial(n - 1)
Saturday, June 4, 2016
python class
https://jeffknupp.com/blog/2014/06/18/improve-your-python-python-classes-and-object-oriented-programming/
- Any class data that is not associated with self - is not used in an instance. It has to do with the data only not with the method
- Any class data that is not associated with self - is not used in an instance. It has to do with the data only not with the method
python generators or python yield
Note: Infinite loop is not an issue for generator. It does not take processor time as the generator object is called only during data retrieval:
def my_gen():
count = 0
while True:
count += 1
yield count + 1
Usage: memory optimized iteration (virtually stores calculated values like list without actually consuming memory: the values are retrieved runtime: kind of creates a memory place holder at a place in memory where a calculation is required and then call whenever it is required) and asyncronous operation
# yield is more than return
return is meant to stop/exit function execution when return keyword is reached. the objective of yield is to return continued values indefinitely. usage of return may look same some time but not always. return is mostly used at the end of the function to return a value and end the function, while yield objective is to continue returning when the values are to be retrieved.
yield => "yield a series of values". Function with yield is called generator because it is used to generate a series of values.
-return gets only once chance to return a value
-For infinite series, if we use return - we have to store all the values in a variable which may be out of memory or impossible to hold : but if the function can return one value at a time by doing all the calculation - then we can deal with very large or infinite numbers. - there is no more memory issue with this approach.
IMPORTANT: During 1st element retrieval, all the lines of the function is called, then (2nd onward only yield (kind of return) values are returned.
http://stackoverflow.com/questions/7883962/where-to-use-yield-in-python-best
http://pymbook.readthedocs.io/en/latest/igd.html
https://jeffknupp.com/blog/2013/04/07/improve-your-python-yield-and-generators-explained/
yield is best used when you have a function that returns a sequence and you want to iterate over that sequence, but you do not need to have every value in memory at once.
yield with respect to coroutines, cooperative multitasking and asynchronous I/O
# About generator:
-a function becomes generator when return is replaced by keyword 'yield'
-when a generator is called first time, it creates a object in memory and does not execute and code in the generator or generator function.
-To use generator
# Simple generator example:
def a():
yield 1
b = a() # generator initialized: a generator cannot be used without initialization like class. Generator initialization look like function call - but both (function and generator) works/behaves differently.
b.next() # retrieving values expected from yield. Once a value is retrieved, the generator object points to next element and the object looses that element after retrieval.
# Understand memory optimized operation using yield
- Often we need to collect collected values in a variable by calling a function and use that data into another function or another place in the script.
Example:
-a = call_function_that_gives_a_list(args1)
-b1 = call function_1( args1)
-b2 = call function_2( args2)
# One use of generator
def get_primes(number):
while True:
if is_prime(number):
yield number
number += 1
def my_gen():
count = 0
while True:
count += 1
yield count + 1
Usage: memory optimized iteration (virtually stores calculated values like list without actually consuming memory: the values are retrieved runtime: kind of creates a memory place holder at a place in memory where a calculation is required and then call whenever it is required) and asyncronous operation
# yield is more than return
return is meant to stop/exit function execution when return keyword is reached. the objective of yield is to return continued values indefinitely. usage of return may look same some time but not always. return is mostly used at the end of the function to return a value and end the function, while yield objective is to continue returning when the values are to be retrieved.
yield => "yield a series of values". Function with yield is called generator because it is used to generate a series of values.
-return gets only once chance to return a value
-For infinite series, if we use return - we have to store all the values in a variable which may be out of memory or impossible to hold : but if the function can return one value at a time by doing all the calculation - then we can deal with very large or infinite numbers. - there is no more memory issue with this approach.
IMPORTANT: During 1st element retrieval, all the lines of the function is called, then (2nd onward only yield (kind of return) values are returned.
http://stackoverflow.com/questions/7883962/where-to-use-yield-in-python-best
http://pymbook.readthedocs.io/en/latest/igd.html
https://jeffknupp.com/blog/2013/04/07/improve-your-python-yield-and-generators-explained/
yield is best used when you have a function that returns a sequence and you want to iterate over that sequence, but you do not need to have every value in memory at once.
yield with respect to coroutines, cooperative multitasking and asynchronous I/O
# About generator:
-a function becomes generator when return is replaced by keyword 'yield'
-when a generator is called first time, it creates a object in memory and does not execute and code in the generator or generator function.
-To use generator
# Simple generator example:
def a():
yield 1
b = a() # generator initialized: a generator cannot be used without initialization like class. Generator initialization look like function call - but both (function and generator) works/behaves differently.
b.next() # retrieving values expected from yield. Once a value is retrieved, the generator object points to next element and the object looses that element after retrieval.
# Understand memory optimized operation using yield
- Often we need to collect collected values in a variable by calling a function and use that data into another function or another place in the script.
Example:
-a = call_function_that_gives_a_list(args1)
-b1 = call function_1( args1)
-b2 = call function_2( args2)
# One use of generator
def get_primes(number):
while True:
if is_prime(number):
yield number
number += 1
Subscribe to:
Posts (Atom)