Wednesday, March 30, 2016

Python Notes

perl to Python notes for my reference:

Some of these are copy paste from other websites
-----------
1)
import re

result = re.sub(pattern, repl, string, count=0, flags=0);


num = re.sub(r'abc(def)ghi', r'\1', input)    # Replace a string with a part of itself

print num

or

num = re.sub(r'abc(def)ghi', r'\1', input)    # Replace a string with a part of itself

-----------

2) IndexError: list assignment index out of range

j = []
k = 0

for l in i:
    j[k] = l
    k += 1
j is an empty list, but you're attempting to write to element [0] in the first iteration, which doesn't exist yet.
Try the following instead, to add a new element to the end of the list:
for l in i:
    j.append(l)
---------------------------

3) How to split a string using an empty separator in Python

>>> list('1111')
['1', '1', '1', '1']
Alternatively, you can use map():

>>> map(None, '1111') ['1', '1', '1', '1']


4) Counting the Number of keywords in a dictionary in python

len(yourdict.keys())
or just
len(yourdict)

Check if a given key already exists in a dictionary

if 'key1' in dict.keys():

Check if a given key not exists in a dictionary

    if 'key1' not in dict1.keys():

----------------------------------
5) File open and read:
obj = open("./abc.txt","r")
print obj.name
var = obj.read() #read everything from file
obj.seek(0,0) #move to beginning of file
first_line = obj.readline() #get line by line

list1 = obj.readlines() #get everything in list

for line in obj:  #iterate line by line
    print line,
---
with open('myfile.txt', 'r') as f:
    first_line = f.readline().rstrip() #rstrip for clearing space or newline at end of line
obj.close();


-------
Note:
If you get below error, you are using like below which is wrong
mixing iteration and read methods would lose data


for line in obj:  #iterate line by line

    obj.readline()

use 

for line in obj:  #iterate line by line


    print line

----------
file_object = open(file_path)
            # Throwing away the first line

            file_object.next()

-------------------------------
6) regex

import re

obj = open("./ijkl",'r+')
fl = obj.readline()
print fl
for i in range(int(fl)):
    line = obj.readline().rstrip()
    print line
    t1 = re.findall(r'(?=[0-9])',line)
    t2 = re.findall(r'(?=[A-Z])',line)
    print "total numbers =", len(t1)
    print "total CAP letters =", len(t2)

----------------------
7) What is the difference between range and xrange functions in Python 2.X?

range - list gets created before looping
xrange - list gets created on the fly (xrange is effective)

--------------------

8) If you wonder why the python file gets executed twice

 It may be the file name
You might have named the file and importing the same.
Solution:
rename the file
remove .pyc file which got created in the same directory

ex:
 getopt.py

  import getopt

--------------------
9) To check if a file exists or not

import os.path
os.path.isfile(fname) 

or

import os.path

os.path.exists(file_path)

or
import os
fname = "foo.txt"
if os.path.isfile(fname):
    print "file does exist at this time"
else:

    print "no such file"

----------------------


10)  concatenate strings

str1 = "Hello"
str2 = "World"
str1 + str 2 # concatenation: a new string 

----------
11)
Write to file:
obj = open("filename.txt","w")
obj.write("hello world")
obj.close()

-----------
12)
Pass elements of a list as arguments to a function in python
args = [1,2,3]
commands(*args)
-------
13) split strings


>> str1 = "a    b     c      d"
>>> re.split(" +", str1)
['a', 'b', 'c', 'd']
However there is no need for regex, str.split without any delimiter specified will split this by whitespace for you. This would be the best way in this case.
>>> str1.split()
['a', 'b', 'c', 'd']
If you really wanted regex you can use this ('\s' represents whitespace and it's clearer):
>>> re.split("\s+", str1)
['a', 'b', 'c', 'd']
or you can find all non-whitespace characters
>>> re.findall(r'\S+',str1)
['a', 'b', 'c', 'd']
 
-----------------
 
 14) Sort by numbers

list1 = ["1","10","3","22","23","4","2","200"]
list1 = [int(x) for x in list1]
list1.sort()
--------------
15) lambda

>>> def f(x):
...     return x*2
...     
>>> f(3)
6
>>> g = lambda x: x*2  1
>>> g(3)
6
>>> (lambda x: x*2)(3) 2
   6
--------------

16)
Loops:

Perl next equals python continue

perl last equals python break


-------------
17) How to pass variable for class name


country = 'Bulgaria'

class BulgariaInstrument(Instrument):
pass


[eval("%sInstrument('%s')" % (country, line))]


-----------------
18) 

>>> class ABC:
...     def __init__(self):
...             self.a = 1
...             self.b = 'hello'
...             self.c = 4.55
... 
>>> ob = ABC()
>>> 
>>> ob.__dict__
{'a': 1, 'c': 4.55, 'b': 'hello'}
>>> dir(ob)
['__doc__', '__init__', '__module__', 'a', 'b', 'c']

the __dict__ attribute is that the instances of the class would have. It's a descriptor object that returns the internal dictionary of attributes for the specific instance.

----------------------
19) 

Introducing getattr

>>> li = ["Larry", "Curly"]
>>> li.pop                       1
<built-in method pop of list object at 010DF884>
>>> getattr(li, "pop")           2
<built-in method pop of list object at 010DF884>
>>> getattr(li, "append")("Moe") 3
>>> li
["Larry", "Curly", "Moe"]
>>> getattr({}, "clear")         4
<built-in method clear of dictionary object at 00F113D4>
>>> getattr((), "pop")           5
Traceback (innermost last):
  File "<interactive input>", line 1, in ?
AttributeError: 'tuple' object has no attribute 'pop'
------------------
20)
has1 = {'abc':1, 'def':2, 'ghi':3}
print "first %(abc)s and second %(def)s and third %(ghi)s" % has1


first 1 and second 2 and third 3



----------------

21) Calling an external command in Python



from subprocess import call
call(["ls", "-l"])



subprocess.call('ls -l', shell=True)
or 
subprocess.call(['ls', '-l'], shell=True)



import subprocess
ls_output = subprocess.check_output(['ls'])
ls_output = subprocess.check_output('get_info -H xxx-xxx-028|grep "LEASE_OWNER"', shell=True)
print ls_output,
 LEASE_OWNER=Arul

===
cat file.log | tail -1
import subprocess
p1 = subprocess.Popen(["cat", "file.log"], stdout=subprocess.PIPE)
p2 = subprocess.Popen(["tail", "-1"], stdin=p1.stdout, stdout=subprocess.PIPE)
p1.stdout.close()  # Allow p1 to receive a SIGPIPE if p2 exits.
output,err = p2.communicate()

import subprocess 
 #output=`dmesg | grep hda`
# becomes 
p1 = subprocess.Popen(["dmesg"], stdout=subprocess.PIPE) 
p2 = subprocess.Popen(["grep", "hda"], stdin=p1.stdout, stdout=subprocess.PIPE) 
p1.stdout.close()  # Allow p1 to receive a SIGPIPE if p2 exits. 
output = p2.communicate()[0] 
print output

----------------


22) How can I remove (chomp) a newline in Python?
>>> 'test string \n'.rstrip('\n')
'test string '
own = own.rstrip('\n')
-----------------
23) Simple python 2d graph using csv file
Input file:
TIME,CPU12,CPU13,CPU14,CPU15
1,1,1,1,1
2,1,1,1,1
3,1,1,1,1
4,28,28,28,29
5,93,92,93,92
6,98,97,98,98
7,93,93,94,93
8,96,95,96,95
9,98,97,98,98
10,97,97,98,98
11,96,96,97,96
12,97,97,98,98
13,96,95,96,96
14,97,97,98,98
15,98,98,99,98
16,97,93,94,94
17,97,97,98,97
18,93,92,94,93
19,97,97,98,98
20,93,92,93,93
21,95,95,96,95
22,96,96,98,97
23,98,97,98,98
24,95,94,96,95
Program:
from matplotlib import pyplot, dates
from csv import reader
from dateutil import parser

with open('/tmp/fpoutput.txt', 'r') as f:
    data = list(reader(f))


time = [i[0] for i in data[1::]]
cpu1 = [i[1] for i in data[1::]]
cpu2 = [i[2] for i in data[1::]]
cpu3 = [i[3] for i in data[1::]]
cpu4 = [i[4] for i in data[1::]]

plot1 = pyplot.plot(time, cpu1, color="blue", linewidth=1.5, linestyle="-", label="cpu12")
plot2 = pyplot.plot(time, cpu2, color="red",  linewidth=1.5, linestyle="-", label="cpu13")
plot2 = pyplot.plot(time, cpu3, color="green",  linewidth=1.5, linestyle="-", label="cpu14")
plot2 = pyplot.plot(time, cpu4, color="orange",  linewidth=1.5, linestyle="-", label="cpu15")

pyplot.title('Plot Title')
# make axis labels
pyplot.xlabel('time')
pyplot.ylabel('cpu usage in %')
pyplot.legend(loc='upper left')

# show the plot on the screen
pyplot.show()

-----------------------------------------
=========================================================================
Previython

1) Formatting a string
> > > x = 2.876543
> > > ’longer: {x:.5f}, shorter: {x:.3f}.’.format(**locals())
> > > ’longer: 2.87654, shorter: 2.877.’

2) Format method:
> > > x = 23.457413902458498
> > > format(x, ’.5f’)
> > > ’23.45741’

3) Python lets you see all the methods that are bound to an object (and any object of its type) with the
built-in function dir. To see all string methods, supply the dir function with any string. For example, try
in the Shell:
dir(’’)

4) if guess == number :
print('Congratulations, you guessed it.') # New block starts here
print('(but you do not win any prizes!)') # New block ends here
elif guess < number:
print('No, it is a little higher than that') # Another block
# You can do whatever you want in a block ...
else:
print('No, it is a little lower than that')

5) while statement can have an optional else clause.
If there is an else clause for a while loop, it is always executed unless you break out of the loop with a break statement

6) for loop:

for i in range(0,10):
print ('{0}) Oh! what a music!'.format(i))
else:
print (‘The for loop is over’)

By default, range takes a step count of 1. If we supply a
third number to range, then that becomes the step count. For example, range(1,5,2)
gives [1,3]. Remember that the range extends up to the second number i.e. it does not
include the second number.

7) break and continue in loops

8) global variable
   global x

9) nonglobal x

10) print(int.__doc__)
     ‘’’ This is doc.

         The explanation starts here.’’’

11) help(int)

12) import sys
sys.argv

13) from sys import argv
     from sys import *

myModule.py
sayhi()
__version__

#import  myModule

myModule.sayhi()
myModule.__version__

#from myModule import sayhi, __version__

sayhi()
__version__

from myModule import *
sayhi()
would not import __version__ since it starts with ‘__’

14) >>> dir(sys)

built-in dir function to list the identifiers that an object defines

15) Packages are just folders of modules with a special __init__.py file that indicates to
Python that this folder is special because it contains Python modules.

16) print('These items are:', end=' ')
DATA STRUCTURE
17) List
shoplist = ['apple', 'mango', 'carrot', 'banana']
shoplist.append('rice')
shoplist.sort()
del shoplist[0]

18) Tuple

zoo = ('python', 'elephant', 'penguin')

print(zoo[2])

19) Print a Dictionary
declare
ab = {}    

ab = { 'Swaroop' : 'swaroop@swaroopch.com',
'Larry' : {‘a’ : ‘apple’, ‘b’ : ‘bat’}
 }

del ab['Swaroop']

for name, address in ab.items():
print('Contact {0} at {1}'.format(name, address))
-----------
for name in ab:
print name
for address in ab[name]:
print address
-------------

# Adding a key-value pair
ab['Guido'] = 'guido@python.org'

if 'Guido' in ab: # OR ab.has_key('Guido')
print("\nGuido's address is", ab['Guido'])

help(dict)

20)
if name.find('war') != -1:
print('Yes, it contains the string "war"')

21)
delimiter = '_*_'
mylist = ['Brazil', 'Russia', 'India', 'China']
print(delimiter.join(mylist))

22)
import os

# Run the backup
if os.system(zip_command) == 0:
print('Successful backup to', target)
else:
print('Backup FAILED')

if not os.path.exists(today):
os.mkdir(today) # make directory

comment = ‘string’

target = today + os.sep + now + '_' + comment.replace(' ', '_') + '.zip'

===========================

Class and objects

You must be wondering how Python gives the value for self and why you don't need to
give a value for it. An example will make this clear. Say you have a class called MyClass
and an instance of this class called myobject. When you call a method of this object as
myobject.method(arg1, arg2), this is automatically converted by Python into
MyClass.method(myobject, arg1, arg2) - this is all the special self is about.
This also means that if you have a method which takes no arguments, then you still have to
have one argument - the self.

Fields - variables

methods - functions

Fields are of two types - they can belong to each instance/object of the class or they can
belong to the class itself. They are called instance variables and class variables
respectively.

#!/usr/bin/python
# Filename: method.py
class Person:
def sayHi(self):
print('Hello, how are you?')
p = Person()
p.sayHi()
# This short example can also be written as Person().sayHi()

The __init__ method is run as soon as an object of a class is instantiated. The method is
useful to do any initialization you want to do with your object.

class Person:
def __init__(self, name):
self.name = name
def sayHi(self):
print('Hello, my name is', self.name)
p = Person('Swaroop')
p.sayHi()

‘abc {0:d} asdf’.format(2)

howMany = staticmethod(howMany)

A static method does not receive an implicit first argument.
|  To declare a static method, use this idiom:
|  
|    class C:
|    def f(arg1, arg2, ...): ...
|    f = staticmethod(f)
|  
|  It can be called either on the class (e.g. C.f()) or on an instance
|  (e.g. C().f()).  The instance is ignored except for its class.

All class members (including the data members) are public and all the methods are
virtual in Python.
One exception: If you use data members with names using the double underscore
prefix such as __privatevar, Python uses name-mangling to effectively make it a
private variable.

This is very important to remember - Python does not automatically call the
constructor of the base class, you have to explicitly call it yourself.

=============
Python has a strong set of introspection features.
Take a look at the following built-in functions:
  • type()
  • dir()
  • id()
  • getattr()
  • hasattr()
  • globals()
  • locals()
  • callable()
type() and dir() are particularly useful for inspecting the type of an object and its set of attributes, respectively.
========
object.__dict__
==========

dir(int)

=========
help(foo)

>>> import string
>>> string.__file__

'/usr/lib/python2.5/string.py'

def dump(obj):
  for attr in dir(obj):
   if hasattr( obj, attr ):
       print( "obj.%s = %s" % (attr, getattr(obj, attr)))

def dump(obj):
 for attr in dir(obj):
print "obj.%s = %s" % (attr, getattr(obj, attr))
==========================

=======================================
To print the string in reverse

def reverse(text):
return text[::-1]
This is extended slice syntax. It works by doing [begin:end:step] - by leaving begin and end off and specifying a step of -1, it reverses a string.
Another way to reverse a list
l = [1,2,3,4,5]
print list(reversed(l))
[5,4,3,2,1]


  1. l = [1,2,3,4,5]
  2. l.reverse()
  3. print l
  4.  
  5. >>> [5,4,3,2,1]

=======================

sleep

#!/usr/bin/python
import time

print "Start : %s" % time.ctime()
time.sleep( 5 )
print "End : %s" % time.ctime()
=====================
Files:
poem = '''\
Programming is fun
'''
f = open('poem.txt', 'w') # open for 'w'riting
f.write(poem) # write text to file
f.close() # close the file
f = open('poem.txt') # if no mode is specified, 'r'ead mode is assumed
by default
while True:
line = f.readline()
if len(line) == 0: # Zero length indicates EOF
break
print(line, end='')
f.close() # close the file
-------------------
# Arul if there is only new line, the length of line is 1
#Arul
print file('test.log').read()
# read() will read the entire contents from a file to a variable
#Arul
with open("C:\\arul\\programs\\python\\outtext1.txt") as f:
for line in f:
    print (line)
----------------
f = open("file.txt")
lines = f.readlines()
f.close()
---------------
with open('C:/path/numbers.txt', 'r') as f:
   lines = f.readlines()
----------------
How to get all the files in a directory:

import os
f = []
for (dirpath, dirnames, filenames) in os.walk(direc):
   print dirpath
   print dirnames
   print filenames

   f.extend(filenames)
   #break #if this line is uncommented all files under sub directories are gathered
-------------------

======================

Pickle

import pickle
f = open(shoplistfile, 'wb')

pickle.dump(object,file_obj)

f = open(shoplistfile, 'rb')
storedlist = pickle.load(file_obj)

=============================

Exceptions:

try:
text = input('Enter something --> ')
except EOFError:
print('Why did you do an EOF on me?')
except KeyboardInterrupt:
print('You cancelled the operation.')
else:
print('You entered {0}'.format(text))

============

sudo python -m pip install selenium

===========
own exception:

class ShortInputException(Exception):
'''A user-defined exception class.'''
def __init__(self, length, atleast):
Exception.__init__(self)
self.length = length
self.atleast = atleast

try:
text = input('Enter something --> ')
if len(text) < 3:
raise ShortInputException(len(text), 3)
# Other work can continue as usual here
except EOFError:
print('Why did you do an EOF on me?')
except ShortInputException as ex:
print('ShortInputException: The input was {0} long, expected at
least {1}'\
.format(ex.length, ex.atleast))
else:
print('No exception was raised.')

==================
finally:

try:
f = open('poem.txt')
while True: # our usual file-reading idiom
line = f.readline()
if len(line) == 0:
break
print(line, end='')
time.sleep(2) # To make sure it runs for a while
except KeyboardInterrupt:
print('!! You cancelled the reading from the file.')
finally:
f.close()
print('(Cleaning up: Closed the file)')

================
The exec function is used to execute Python statements which are stored in a string or file,

>>> exec('print("Hello World")')
Hello World

===================

>>> eval('2*3')
6

================
assert

The assert statement is used to assert that something is true. For example, if you are very
sure that you will have at least one element in a list you are using and want to check this,
and raise an error if it is not true, then assert statement is ideal in this situation. When
the assert statement fails, an AssertionError is raised.

>>> mylist = ['item']
>>> assert len(mylist) >= 1
>>> mylist.pop()
'item'
>>> mylist
[]
>>> assert len(mylist) >= 1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError

===================

Real life problems

Without the 0x prefix, you need to specify the base explicitly, otherwise there's no way to tell:
x = int("deadbeef", 16)
With the 0x prefix, Python can distinguish hex and decimal automatically:
>>> print int("0xdeadbeef", 0) 3735928559 >>> print int("10", 0) 10
===============

==============================================================================

REGULAR Expression
  1. To find all the matches
import re

var = '10.34.56.74 asdf 3.3.3.2 rerd 3.12.234.221 adfkje 4.3.221.3'
mylist = re.findall('(\d+\.\d+\.\d+\.\d+)',var)
print (mylist)

2) To just find if it exists

import re

var = '10.34.56.74 asdf 3.3.3.2 rerd 3.12.234.221 adfkje 4.3.221.3'
mylist = re.search('(\d+\.\d+\.\d+\.\d+)',var)
print (mylist.group())

3) To find a match at the begining

var = '10.34.56.74 asdf 3.3.3.2 rerd 3.12.234.221 adfkje 4.3.221.3'
mylist = re.match('(\d+\.\d+\.\d+\.\d+)',var)
print (mylist.group())
4)
import re

var = '10.34.56.74 asdf 3.3.32233.2 rerd 3.12.234.221 adfkje 4.3.221.3'
mylist = re.findall('(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})',var)
print (mylist)

for eachip in mylist:
    print (eachip)
    octs = re.split("\.",eachip)
    print (octs)

5) re.compile()

prog = re.compile(pattern)
result = prog.match(string)
is equal to
result = re.match(pattern, string)
but using re.compile() and saving the resulting regular expression object for reuse is more efficient when the expression will be used several times in a single program.

6)
re.I - Ignore case
re.M - Multiline - ‘^’ character matches beginning of multi line
re.S  - '.' special character match any character at all, including a newline;
re.U - unicode
re.X - Verbose - look nicer

7) re.search(pattern, string, flags=0)
- Scan through string looking for the first location where the regular expression pattern produces a match, and return a corresponding MatchObject instance. Return None if no position in the string matches the pattern;

8) re.match(pattern, string, flags=0)
If zero or more characters at the beginning of string match the regular expression pattern

9) re.split(pattern, string, maxsplit=0, flags=0)
Split string by the occurrences of pattern. If capturing parentheses are used in pattern, then the text of all groups in the pattern are also returned as part of the resulting list. If maxsplit is nonzero, at most maxsplit splits occur, and the remainder of the string is returned as the final element of the list.
10) re.findall(pattern, string, flags=0)
Return all non-overlapping matches of pattern in string, as a list of strings. The string is scanned left-to-right, and matches are returned in the order found.

11) re.finditer(pattern, string, flags=0)
Return an iterator yielding MatchObject instances over all non-overlapping matches for the RE pattern in string.
12) re.sub(pattern, repl, string, count=0, flags=0)
  replaces all the matches found
13) re.subn(pattern, repl, string, count=0, flags=0)
Perform the same operation as sub(), but return a tuple (new_string, number_of_subs_made).
14) re.purge()
Clear the regular expression cache.
15) re.escape(string)
Return string with all non-alphanumerics backslashed; this is useful if you want to match an arbitrary literal string that may have regular expression metacharacters in it.

==========================
fork example:

import os, time

NUM_PROCESSES = 7

def timeConsumingFunction():
  x = 1
  for n in xrange(10000000):
      x += 1

  return x

children = []

start_time = time.time()
print('start_time: ',start_time)
for process in range(NUM_PROCESSES):
  pid = os.fork()
  if pid:
      children.append(pid)
      print('i am parent, i created child ',pid)
  else:
      y = timeConsumingFunction()
      print(y)
      os._exit(0)

for i, child in enumerate(children):
  print('waiting for child: ',child)
  os.waitpid(child, 0)
  print('Done with child: ',child)

print time.time() - start_time
=================================================
How to round off a number

import math
v = 8.8333333333333339
print(math.ceil(v*100)/100) # -> 8.84
==================================================
How do I check whether an int is between the two numbers?

if 10 <= number <= 30:
pass
Note: 10 should be less or equal to number
     Number should be less or equal to 30 =============================================

[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
Interesting things: Better coding
a = [2,3,4,54,9,12,3]
for i, number in enumerate(a):
   print i, " ", number
0   2
1   3
2   4
3   54
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
a = [2, 3, 4, 6, 7]
b = ["a", "b", "c", "d"]
for x,y in zip(a,b):
   print x, " ",y
Ans:
2   a
3   b
4   c
6   d
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
Swapping:
X = 10
Y = -10
Ans:
x,y = y,x
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
a = {"d":1, "e":2}
if "c" in a:
   print a["c"]
else:
   print "Not found"
print a.get("c","Not found")
Ans:
Not found
Not found
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
with open("./text.file","r+") as f:
   for lines in f:
       print lines,
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
try:
   int('1')
except:
   print "Error occured"
else: # opposite of except
   print "passed"
finally: # gets executed always
   print "All done"
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
Array left rotation:

def array_left_rotation(a, n, k):
   abc = k%n
   first = a[abc:]
   second = a[:abc]
   first.extend(second)
   #print first
   return first
   
   
n, k = map(int, raw_input().strip().split(' '))
a = map(int, raw_input().strip().split(' '))
answer = array_left_rotation(a, n, k);
print ' '.join(map(str,answer))
<<>>
Sample Input
5 4
1 2 3 4 5
Sample Output
5 1 2 3 4
==========================================
How to find duplicates in a list
from collections import Counter
abc = [1,2,1,3,3,2,5,2]
dict(Counter(abc))
{1: 2, 2: 3, 3: 2, 5: 1}
list(Counter(abc))
[1, 2, 3, 5]
----------
Same as
a = ["a","ab","a","b","ef","ef","b"]
g = {}
for i in a:
g[i] = g.get(i,0) + 1
output:
{'a': 2, 'ab': 1, 'b': 2, 'ef': 2}
----------

>>> abc = [1, 2, 1, 3, 3, 2, 5, 2]
>>> ggg = [1, 2, 1, 2, 5, 2, 3, 3]
>>> Counter(abc) - Counter(ggg) == {}
True
===================================
Change Negative to Positive

>>> xx = -45.5555
>>> abs(xx)
45.5555
=========================
>>> range(5, 1,-1)
[5, 4, 3, 2]
====================
Sort based on particular column in list of lists
from operator import itemgetter
>>> xy = [[1,2,3],[4,5,6],[10,11,12],[7,8,9],[3,4,3],[1,2,4]]
>>> sorted(xy,key=itemgetter(0))
[[1, 2, 3], [1, 2, 4], [3, 4, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
>>> sorted(xy,key=itemgetter(0),reverse=True)
[[10, 11, 12], [7, 8, 9], [4, 5, 6], [3, 4, 3], [1, 2, 3], [1, 2, 4]]
>>>
============================
import random
for i in xrange(random.randint(1,200)):
print i
>>> a = [1,2,3,4,5,6]
>>> random.choice(a)
5
=============================
Combine muliple list and iterate
Chain:
>>> my_list = ['foo', 'bar']
>>> numbers = list(range(5))
>>> cmd = ['ls', '/some/dir']
>>> from itertools import chain
>>> my_list = list(chain(['foo', 'bar'], cmd, numbers))
>>> my_list
['foo', 'bar', 'ls', '/some/dir', 0, 1, 2, 3, 4]
================================
Groupby Example:
from itertools import groupby

vehicles = [('Ford', 'Taurus'), ('Dodge', 'Durango'),
           ('Chevrolet', 'Cobalt'), ('Ford', 'F150'),
           ('Dodge', 'Charger'), ('Ford', 'GT')]

sorted_vehicles = sorted(vehicles)

for key, group in groupby(sorted_vehicles, lambda make: make[0]):
   for make, model in group:
       print('{model} is made by {make}'.format(model=model,
                                                make=make))

Cobalt is made by Chevrolet

Charger is made by Dodge
Durango is made by Dodge

F150 is made by Ford
GT is made by Ford
Taurus is made by Ford
===========================
>>>from itertools import combinations
>>>a = list(combinations('WXYZ', 2))
[('W', 'X'), ('W', 'Y'), ('W', 'Z'), ('X', 'Y'), ('X', 'Z'), ('Y', 'Z')]

>>> for i in a: ... "".join(i) ... 'wx' 'wy'


==============================


=============================
Is there a function in python to split a word into a list?

list("Word to Split")
s = "Word to Split" wordlist = list(s) # option 1, wordlist = [ch for ch in s] # option 2, list comprehension.
They should both give you what you need:
['W','o','r','d',' ','t','o',' ','S','p','l','i','t']

================================
To import multiple methods

from numpy.testing import ( TestCase, run_module_suite, assert_, assert_equal, assert_raises, assert_array_equal, assert_almost_equal, assert_array_almost_equal, dec, assert_allclose, assert_no_warnings, suppress_warnings )

=================================
Exception:

import sys

try:
    print "hello"
    raise KeyError("i am ","the world")
except KeyError as f:
    print "E"
    print f
  
except:
     print "Unexpected error:", sys.exc_info()

    
>>hello
>>E
>>('i am ', 'the world')
Unexpected error: (<type 'exceptions.IOError'>, IOError(2, 'No such file or directory'), <traceback object at 0x1817a86cf8>)

=================

To the check the return type

>>> isinstance([1,2,3],list)
True

if isinstance(2,int):
    print "It's an integer"
======================
How to maintain the order in dictionary?

# Enter your code here. Read input from STDIN. Print output to STDOUT
from collections import OrderedDict
ind = int(raw_input().rstrip())
dict1 = OrderedDict()

for i in xrange(ind):
    st = raw_input().rstrip()
    dict1[st] = dict1.get(st,0) + 1
   
print len(dict1.keys())
for i,j in dict1.items():
    print j,
     


Sample Input
4
bcdef
abcdefg
bcde
bcdef
Sample Output
3
2 1 1 
========================= 
 
Regex Example match:
#(+90, +180)
#(90, 180)
#(-90.00000, -180.0000)
#(75, 280) 
 
import re

tests = [raw_input() for _ in range(int(raw_input()))]

p = re.compile(r'^\([+-]?(?P<first>(0|([1-9]\d*))(.\d+)?),\s[+-]?(?P<second>(0|([1-9]\d*))(.\d+)?)\)$')

for s in tests:
    m = re.match(p, s)
    if m:
        a, b = float(m.group('first')), float(m.group('second'))
        if a >= 0 and a <= 90 and b >= 0 and b <= 180:
            print "Valid"
            continue
    print "Invalid"
 
=============================
 Exceptions:
Input: 
3
1 0
2 $
3 1
 
Program: 
 
# Enter your code here. Read input from STDIN. Print output to STDOUT
import sys

num = int(raw_input().rstrip())

for _ in xrange(num):
    a,n = raw_input().rstrip().split()
    try:
        a,n = [int(a),int(n)]
        v = a/n
        print v
    #Catch Multiple exception
    #except (ValueError,ZeroDivisionError) as e:
    #    print "Error Code: %s" % e
    # Catch any exception
    except:
        print "Error Code: %s" % sys.exc_info()[1]
 
Output:
Error Code: integer division or modulo by zero
Error Code: invalid literal for int() with base 10: '$'
3 

sys.exc_info()[0] prints type of exception
<type 'exceptions.ZeroDivisionError'>
<type 'exceptions.ValueError'>
 
sys.exc_info()[1] prints exception error message
integer division or modulo by zero
invalid literal for int() with base 10: '$' 
  
===================== 
Get all the diagonals in a matrix/list of lists in Python
import numpy as np

matrix = np.array(
         [[-2,  5,  3,  2],
          [ 9, -6,  5,  1],
          [ 3,  2,  7,  3],
          [-1,  8, -4,  8]])

diags = [matrix[::-1,:].diagonal(i) for i in range(-3,4)]
diags.extend(matrix.diagonal(i) for i in range(3,-4,-1))
print [n.tolist() for n in diags]

Output

[[-2], [9, 5], [3, -6, 3], [-1, 2, 5, 2], [8, 7, 1], [-4, 3], [8], [2], [3, 1], [5, 5, 3], [-2, -6, 7, 8], [9, 2, -4], [3, 8], [-1]]
===================== 
To find beginning and ending index of a word in a string
import re
text = 'You can try to find an ant in this string'
pattern = 'an?\w' # find 'an' either with or without a following word character
for match in re.finditer(pattern, text): 
    # Start index of match (integer) 
    sStart = match.start()
    # Final index of match (integer)
    sEnd = match.end()
    # Complete match (string)
    sGroup = match.group()
    # Print match
    print('Match "{}" found at: [{},{}]'.format(sGroup, sStart,sEnd)) 

===
  
Result:
Match "an" found at: [5,7]
Match "an" found at: [20,22]
Match "ant" found at: [23,26]

=================
Regex Fun:
5 x 5 matrix
      [[5, 5, 5, 4, 5],
       [6, 7, 8, 9, 9],
       [9, 2, 3, 4, 5],
       [6, 7, 1, 4, 6],
       [1, 2, 3, 4, 8]]

5554567899923456714612348
 
Write a RegEx to determine if  has  or more consecutive repeating digits, 
which would correspond to a horizontal or vertical selection of the grid
 
Regex_Pattern = r"^.{0,2}(.{5})*(.)\2\2|(.).{4}\3.{4}\3" # Do not delete 'r'.
 
===================
Regex repeated character match
a = re.findall(r"((\w)\2*)", "AAABBCCB")
print a
[('AAA', 'A'), ('BB', 'B'), ('CC', 'C'), ('B', 'B')] 
===================