Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Resources:
- # Minimal Python Datastructures and Algorithms examples: https://github.com/keon/algorithms
- #
- #
- "harsh trivedi".capitalize() #=> "Harsh trivedi"
- "harsh trivedi".upper() #=> 'HARSH TRIVEDI'
- "Harsh Trivedi".lower() #=> 'harsh trivedi'
- "harsh"[start:end:hop]
- "harsh"[start:end:hop]
- # hop can be -ve as well, in which case it starts from end
- some_string[-1] # last item in the array
- some_string[-2:] # last two items in the array
- some_string[:-2] # everything except the last two items
- some_string[::-1] # To reverse the string
- # Destructive Sort
- sorted([4,1,2,3])
- # > [1,2,3,4]
- sorted( [4,1,2,3] , key = lambda x: x, reverse = True)
- # > [4,3,2,1]
- # Inplace Sort
- array = [4,1,2,3]
- array.sort()
- array = [4,1,2,3]
- array.index(2)
- array = [4,1,2,3]
- array.pop() #> 3
- # array => [4,1,2]
- array = [0,1,2,3,4,5,6]
- array.pop(3) #> 3
- # array => [0,1,2,4,5,6]
- array = [0,1,2,3,4,5,6]
- array.pop(-1) #> 6
- # array => [0,1,2,4,5]
- range(15)
- # [0, 1, 2 ... 14]
- import random
- random.random() #> random number in 0-1
- random.randint(1,100) # randome integer between 1 to 100
- import re
- line = "Cats are smarter than dogs"
- match = re.match( r'(.*) are (.*?) .*', line )
- print "match.group({})".format( match.group(1) )
- # match.group(0) #=> Cats are smarter than dogs
- # match.group(1) #=> Cats
- # match.group(2) #=> smarter
- # match.group(3) #=> Error !
- math.abs()
- math.ceil()
- math.floor()
- map_plus_one = [x+1 for x in range(10)]
- map_plus_one = map( lambda x: x+1, range(10))
- # set intersection of list
- intersection = set.intersection(set(list1), set(list2))
- union = set.union(set(list1), set(list2))
- difference = set.difference(set([1,2,3,4]), set([2,3,4,5]))
- # split by constant string
- "harsh trivedi".split() #=> ['harsh', 'trivedi']
- "harsh trivedi".split(' ') #=> ['harsh', 'trivedi']
- # split by regex
- re.split(" +", "harsh trivedi") #=> ['harsh', 'trivedi']
- # replace by constant string
- "harshtrivedi".replace("trivedi", '') #=> 'harsh'
- # replace regex by string
- re.sub( ' +', ' ', 'harsh trivedi') #=> 'harsh trivedi'
- # replace regex by what was captured
- processed = re.sub( '(\w+) + (\w+)', r'\1 \2', 'harsh trivedi' )
- #=> harsh trivedi
- # find all matches of regex match in a list
- re.findall( 'har?sh', "harsh trivedi harsh trivedi harsh trivedi trivedi harsh")
- # => ['harsh', 'harsh', 'harsh', 'harsh']
- # flatten list
- [ element for sub_list in nested_array for element in sub_list]
- # Deep Flatten List:
- def flatten(lol):
- return sum( [flatten(l) for l in lol ], [] ) if isinstance( lol, (tuple,list) ) else [lol]
- # multi level list comprehension
- non_primes = [j for i in range(2, 8) for j in range(i*2, 50, i)]
- right_triangle_triples = [(x,y,z) for x in range(1,30) for y in range(x,30) for z in range(y,30) if x**2 + y**2 == z**2]
- add_one = map(lambda x: x+1, [1,2,3,4,5] )
- even = filter(lambda x: x%2==0, [1,2,3,4,5] )
- addition = reduce(lambda x,y: x+y, [47,11,42,13])
- # reduce takes one additional start (optional) parameter:
- reduce(lambda x,y: x+y, [1,2,3,4] ) #=> 10
- reduce(lambda x,y: x+y, [1,2,3,4], 0 ) #=> 10
- reduce(lambda x,y: x+y, [1,2,3,4], 1 ) #=> 11
- # reduce can be applied to lists as well:
- reduce(lambda x,y: x+y, [[1],[2],[3],[4]], [] ) #=> [1, 2, 3, 4]
- reduce(lambda x,y: x+y, [ 'har', 'sh', 'tr', 'ivedi'], '' ) #=> harshtrivedi
- reduce(lambda x,y: x*y, [ 1, 2, 3, 4, 5], 1) #=> 12-
- # building product lambda from above:
- product = lambda l: reduce(lambda x,y: x*y, l, 1)
- product([ 1, 2, 3, 4, 5]) #=> 120
- "harshtrivedi".find('trivedi') #=> 5
- "harshtrivedi".find('xyz') #=> -1
- "harshtrivedi".index('trivedi') #=> 5
- "harshtrivedi".index('xyz') #=> Error!
- # Note that find finds first occurrence from left. There is rfind as well (but index is always from left)
- path = 'toto.titi.tata..xls'
- path.find('.') #=> 4
- path.rfind('.') #=> 15
- # enumerate with index [Default it starts from 0 index]
- for index, element in enumerate(array):
- print (index, element)
- # enumerate with some other index k
- for index, element in enumerate(array, k):
- print (index, element)
- # Set X is subset of Set Y
- set(X) <= set(Y)
- { "{}".format(i) for i in range(2, 10) } # => is set
- { "{}".format(i):i for i in range(2, 10) } # => is dict
- # Using Lambda Functions:
- lambda_sum = lambda arg1, arg2: arg1 + arg2;
- print lambda_sum( 10, 20 )
- # btw, sum is present inbuilt
- sum([1,2,3,4,5]) #= 15
- # you can pass second argument on tell it where to start from
- sum([1,2,3,4,5], 10) #= 25
- # Interestingly it can sum lists using + operatros as well
- sum([ [1], [2,3,4], [5], [6] ], []) #=> [1,2,3,4,5,6]
- # But this doesn't apply to strings. Use ''.join([ ... ]) instead
- sum([ 'one', 'two', 'three', 'four' ], "") #=> ERROR!!!
- " ".join( ["1", "2", "3"] ) # => '1 2 3'
- " ".join( [1, 2, 3] ) # => Error !
- " ".join( map(str, [1, 2, 3]) ) # => '1 2 3'
- # Using map/filter/reduce with instance method is also possible.
- map(lambda foo: foo.get_bar(), foos) # get_bar() is an instance method of foo: Usage: foo.get_bar()
- map(bar, foos) # bar() is method in main: Usage: bar(foo)
- # Some Useful List Comprehensions
- # To split list / string in chunks of size k
- [my_string[i:i + k] for i in xrange(0, len(my_string), k)]
- # Note: (for i in range(10))
- # returns an generator. So you can do:
- iterator = (for i in range(10))
- iterator.next() #=> 0
- iterator.next() #=> 1
- iterator.next() #=> 2
- # Read File Lines:
- with open("filename", 'r') as f:
- for line in f.readlines():
- line.strip()
- # operate on line here
- # Write File:
- with open("filename", 'w') as f:
- f.write( "\n".join(lines) )
- # Actual Default Dicts:
- from collections import defaultdict
- someddict = defaultdict(int)
- someddict[1] #=> 0
- someddict[3] += 1
- someddict[1] #=> 1
- someddict = defaultdict(list)
- someddict[1] #=> []
- someddict[1].append(3)
- someddict[1] #=> [3]
- someddict = defaultdict(dict)
- someddict[1] #=> {}
- someddict[1][3] = "Three"
- someddict[1] #=> {3: 'Trhee'}
- # Counting
- count_dict = {}
- colors = ['blue', 'green', 'red', 'green', 'blue']
- for color in colors:
- count_dict[color] = count_dict.get(color, 0) + 1
- # count_dict => {'blue': 2, 'green': 2, 'red': 1}
- # Using Else with For !: Else block will be executed if for didn't break anywhere
- for n in range(2, 10):
- for x in range(2, n):
- if n % x == 0:
- print( n, 'equals', x, '*', n/x)
- break
- else:
- # loop fell through without finding a factor
- print(n, 'is a prime number')
- # Append for unique Default
- dict_list = {}
- colors = ['blue', 'green', 'red', 'green', 'blue']
- for color in colors:
- dict_list[color] = dict_list.get(color, []) + [color]
- # dict_list => {'blue': ['blue', 'blue'], 'green': ['green', 'green'], 'red': ['red']}
- dictionary = { 1: 'one', 2: 'two', 3: 'three'}
- # iterating dicts
- for key, val in dictionary.iteritems():
- print '({}, {})'.format(key, val)
- dictionary.items() #=> [(1, 'one'), (2, 'two'), (3, 'three')]
- # convert dict to list
- list_dict = [(1, 'one'), (2, 'two'), (3, 'three')]
- dict(list_dict) #=> { 1: 'one', 2: 'two', 3: 'three'}
- list_dict = [[1, 'one'], [2, 'two'], [3, 'three']]
- dict(list_dict) #=> { 1: 'one', 2: 'two', 3: 'three'}
- # Set A is Subste of Set B:
- set(A) <= set(B)
- # clear unicode ...
- #
- # Dict Comprehensions
- list_dict = [(1, 'one'), (2, 'two'), (3, 'three')]
- {key: value for (key, value) in list_dict } # {1: 'one', 2: 'two', 3: 'three'}
- # Set Comprehensions
- set_comp = {key for key in iterable}
- {key for key in [1,2,3,4,5,6,3,2,4,5,1] } #=> set([1, 2, 3, 4, 5, 6])
- # check if string is substring
- "harsh" in "harsh trivedi" #=> True
- "xyz" in "harsh trivedi" #=> False
- # substring count by constant string
- "harsh trivedi harsh trivedi harsh".count('harsh')
- # substring count by regex match
- len(re.findall( 'h?r?sh', "harsh trivedi harsh trivedi harsh trivedi trivedi harsh")) #=> 3
- # finditer can also be used
- # Count
- List.count(K) # Counts occurrences of K in list L
- # String Formatters
- "%.3f" % 1 #=> 1.000
- "%03d" % 1 #=> 001
- "%03d" % 1 #=> 001
- "%-3d" % 1 #=> '1 '
- "%03d" % 341 #=> 341
- # Insert element in list at specific index
- array = [1,2,4,5]
- array.insert(2,3) # Returns None
- # array #=> [1, 2, 3, 4, 5]
- # Bisect the sorted list
- import bisect
- sorted_array = [1, 2, 3, 4, 5, 6, 7]
- appr_index = bisect.bisect( sorted_array, 4.5)
- # is equivalent to
- sorted_array = [1, 2, 3, 4, 5, 6, 7] ; f = 4.5
- appr_index = next( ( i for i, e in enumerate(sorted_array) if e >= f), None )
- # Maintaining sorted array from scratch
- sorted_array = []
- for _ in range(10):
- k = random.randint(1, 10)
- index = bisect.bisect(sorted_array, k)
- sorted_array.insert(index, k)
- # sorted_array #=>? [2, 2, 3, 3, 4, 5,7,8,10]
- # Strange But (might be) Useful
- 3 + (2>1) #= 4
- 3 + (False) #= 3
- # Dict Construction from key and val lists:
- my_dict = dict(zip(keyslist, valslist))
- my_dict.copy( ) # shallow copy of dict
- my_dict1.update(my_dict2) # dict merging
- my_dict.has_key('eggs') # check membership of eggs
- # Given path. create all sub-directories as required:
- if not os.path.exists(directory):
- os.makedirs(directory)
- # Remove by val in list:
- some_list = [1,2,3,4,5,2]
- some_list.remove(2)
- # => some_list = [1,3,4,5,2]
- updated_list = filter(lambda e: e != 2, some_list)
- # => updated_list = [1,3,4,5]
- # remove element at given index:
- some_list = [4,3,1,5,2]
- del_index = 3
- del some_list[del_index]
- #=> some_list = [4,3,1,2]
- # remove elements by list of indices
- some_list = [4,3,1,5,2]
- del_indices = [2,1,4]
- for del_index in del_indices:
- some_list[del_index] = None
- del_list = filter(bool, some_list)
- # iterate through all the files in pattern of path
- import glob
- for img in glob.glob("some_path/*.jpg"):
- # process img
- pass
- # Using Collections:
- import collections
- defaultdict
- ordereddict
- dequeue
- counter
- Others:
- glob
- shutil
- # Object Oriented Python
- # Amazing post here (Covers everything)
- # https://jeffknupp.com/blog/2014/06/18/improve-your-python-python-classes-and-object-oriented-programming/
- class Car(object):
- wheels = 4 # Class Attribute
- def __init__(self, make, model):
- self.make = make
- self.model = model
- def __str__(self): #
- return "({}):({})".format(self.make, self.model)
- @staticmethod
- def make_car_sound(): # Class Method
- print 'VRooooommmm!'
- def full_name(self): # Instance Method
- return "Model: {} ; Make: {}".format( self.model, self.make)
- car = Car('Ford', 'Mustang')
- print car.wheels #=> 4
- print Car.wheels #=> 4
- Car.wheels = 3
- print Car.wheels #=> 3
- Car.make_car_sound() #= 'VRooooommmm!'
- print car #= '(Ford):(Mustang)'
- str(car) #=> '(Ford):(Mustang)'
- car.full_name() #=> 'Model: Mustang ; Make: Ford'
- # ###########################
- # Pick random element from a list
- import random
- random.sample(some_list, 1)[0] #=> random element picked from list
- # Pick K random elements from a list
- random.sample(some_list, k) #=> list containing k random elements
- # shuffle a list (destructively)
- some_list = [1, 2, 3, 4, 5]
- random.shuffle(some_list)
- # some_list #=> [4, 3, 2, 1, 5]
- # Itertools:
- itertools.groupby( 'AAAABBBCCDAABBB' ) #=> returns generator with following properties
- # [k for k, g in groupby('AAAABBBCCDAABBB')] --> A B C D A B
- # [list(g) for k, g in groupby('AAAABBBCCD')] --> AAAA BBB CC D
- # Functional Python Awesomeness:
- # http://kachayev.github.io/talks/uapycon2012/#/1
- # Ruby equivalent of compact
- filter(bool, [1,2, "", None, [ ] ]) #=> [1, 2]
- bool("") #=> False
- bool([]) #=> False
- bool(None) #=> False
- # Todo : https://stackoverflow.com/questions/38987/how-to-merge-two-dictionaries-in-a-single-expression
- # IterTools: https://docs.python.org/2/library/itertools.html
- # BitOperations: http://www.onjava.com/pub/a/python/excerpt/PythonPocketRef/index.html
- # Mutation: http://book.pythontips.com/en/latest/mutation.html
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement