Skip to main content

Posts

Showing posts from November, 2018

Python List Functions

Python List functions do quite a job for simple tasks. Though I would recommend Numpy arrays if the application is required to be efficient. Go through the functions available to work on Python Lists from sys import getsizeof print ( "create list" ) a = [ 'a' , 'b' ] print ( a ) print ( "space allocated" ) print ( getsizeof ( a )) print ( getsizeof ( a [ 0 ])) print ( getsizeof ( a [ 1 ])) print ( "add element to list" ) a.append ( 'c' ) print ( a ) print ( "add element at a specific location" ) a.insert ( 1 , 'd' ) print ( a ) print ( "add list at the end of a list" ) print ( a+ [ 'e' , 'f' ]) a.extend ([ 'e' , 'f' ]) print ( a ) print ( "remove an element" ) ( a.remove ( 'e' )) if 'e' in a else ( print ( "not in list" )) print ( a ) print ( "index of an element using index func...

Python - Find common elements in two lists, avoid None values

Finding common values between two lists is easy in Python when compared to Javascript. First remove all None values. Because the in-built function will consider None also as a value to compare. If we don't want that then remove it using filter function. Then make one of the lists as set and use intersection function as follows: Run the code here:  https://repl.it/@VinitKhandelwal/CommonElementsList list1 = [ 'a' , 'b' , 'c' , None ] list2 = [ 'c' , 'd' , 'e' , None ] list3 = list ( filter ( lambda x : x is not None , list1)) list4 = list ( filter ( lambda x : x is not None , list2)) print (list3) print (list4) print ( list ( set (list3).intersection(list4))) list1 = [ 1 , 2 , 3 , 4 , None , 5 , 6 ] list2 = [ 3 , 5 , 7 , 9 , None ] list3= [] for i in list1 : for j in list2 : if i is not None and i == j : list3.append ( i ) break print ( f "{list3} - funct...

Numpy - Multi-dimensional arrays and functions to manipulate them

Numpy offers very efficient functions to manipulate data in multi-dimensional arrays. Here are a few common ones. Use them instead of regular python functions to reduce code length as well as produce efficient and fast programs. import numpy as np a = np.array([( 1 , 2 , 3 , 4 ),( 3 , 4 , 5 , 6 ),( 7 , 8 , 9 , 10 )]) c = np.array([( 10 , 9 , 8 , 7 ),( 6 , 5 , 4 , 3 ),( 4 , 3 , 2 , 1 )]) print (a) print ( "find dimension of array" ) print (a.ndim) print ( "find byte size of array" ) print (a.itemsize) print ( "size of entire array" ) print (a.size) print ( "find data type of elements" ) print (a.dtype) print ( "shape of array" ) print (a.shape) print ( "reshape" ) b = a.reshape( 4 , 3 ) print (b) print ( "get value from a place" ) print (a[ 1 , 2 ]) print ( "get values from same column of first two rows" ) print (a[ 0 : 2 , 2 ]) print ( "equal spacing betw...

Python - Numpy Array is More Processing Efficient than Python List

Numpy array is more efficient than native python list. Run this example to see it for yourself how quick numpy array is when compared to native python list. import numpy as np import time SIZE = 1000 l1 = range (SIZE) l2 = range (SIZE) a1 = np.arange(SIZE) a2 = np.arange(SIZE) t1 = time.time() result1 = [(x+y) for x,y in zip (l1,l2)] t2 = time.time() print ((t2-t1)* 100000 ) t1 = time.time() result2 = a1+a2 t2 = time.time() print ((t2-t1)* 100000 )

Python - Numpy Array is More Memory Efficient than Python List

Numpy array is more efficient than native python list. Run this example to see it for yourself how little memory space numpy array takes when compared to native python list. import numpy as np from sys import getsizeof print (np.array([( 1 , 2 , 3 ),( 4 , 5 , 6 )])) a = range ( 1000 ) b = getsizeof( 1 )* len (a) print (b) c = np.arange( 1000 ) d = c.size*c.itemsize print (d)

Python - A better way to compare and find the latest and the oldest tweet from a list of tweets

One of the solutions I read for the problem of finding the latest and the oldest tweet from a list of tweet was to compare each tweet's date with every other tweet's date. I found this very inefficient and wrote a better way to do the same in the below example. list1 = [{ "tweet" : "tweet1" , "date" : 2018 },{ "tweet" : "tweet2" , "date" : 2016 },{ "tweet" : "tweet3" , "date" : 2017 },{ "tweet" : "tweet4" , "date" : 2019 },{ "tweet" : "tweet5" , "date" : 2015 },{ "tweet" : "tweet6" , "date" : 2014 }] date1 = 2020 date2 = 2000 firsttweet = "" lasttweet = "" for a in list1: print (date1) print (date2) if a[ "date" ]<date1: date1=a[ "date" ] firsttweet=a[ "tweet" ] if a[...

Big O(n!) Example

Big O(n!), you will never find being used in coding. It is the most expensive time complexity. Here is an example that weighs O(n!) class Abc : @ classmethod def forloop ( cls , a , b , endrange , d ): for c in range ( 0 ,endrange): print ( " {} {} {} " .format(a, b, c)) d += 1 print (d) return d xyz = Abc() d = 0 endrange = 5 for a in range ( 0 ,endrange): for b in range ( 0 ,endrange): e = xyz.forloop(a, b, endrange, d) d = e

Python - find index of an element in list

Answer to the python question asked for finding the index of an element in a list class FindingElement : def __init__ ( self ): self .list1 = [ "demo" , "nemo" , "memo" ] def find_element ( self , ele ): try : return "Found {} at index {} " .format(ele, self .list1.index(ele)) except ValueError : return " {} not found" .format(ele) obj = FindingElement() isTrue = True while isTrue: findvar = input ( "What do you want to search? " ) print ( "Type of input: {} " .format( type (findvar))) print ( "Is it a string? {} " .format( isinstance (findvar, str ))) print (obj.find_element(findvar)) try : isTrue = bool ( int ( input ( "Continue? 1 for yes, 0 for no: " ))) except ValueError : p rint ( "Incorrect Input. Continuing with the program....

Python - Marshmallow - Serializing

Create a file name serializing.py from marshmallow import Schema, fields # Install marshmallow latest pre-release version: $ pip install -U marshmallow --pre class BookSchema ( Schema ): # Class of type Schema named BookSchema title = fields.Str() # Schema property author = fields.Str() # Schema property class Book : # Class of type Regular named Book def __init__ ( self , title , author , description ): # Object constructor self .title = title # Class property self .author = author # Class property self .description = description # Class property book = Book( "Clean Code" , "Bob Martin" , "A book aout writing clean code." ) # Creating an object of class Book by passing values required by constructor book_schema = BookSchema() # Creating an object of class BookSchema book_dict = book_schema.dump(book) # Calling dump function of object book_schema of class BookSchema and passing the object book of class Book print ...

Python - Marshmallow - Deserializing

Create a file named deserializing.py from marshmallow import Schema, fields, INCLUDE, EXCLUDE # Install marshmallow latest pre-release version: $ pip install -U marshmallow --pre class BookSchema ( Schema ): # Class of type Schema title = fields.Str( required = True ) # Schema property, required author = fields.Str() # Schema property description = fields.Str() # Schema property class Book : # Class of type Regular def __init__ ( self , title : str , author : str , description : str ): # Object constructor self .title = title # Class property self .author = author # Class property self .description = description # Class property def display ( self ) -> dict : # function that returns a dictionary of all the properties in the object return { "title" : self .title, "author" : self .author, "description" : self .description} # dictionary of all the properties in the ...

Python Flask - Hinting Developers Input and Return Types - Best Practice

Use 'typing' library to hint co-developers reading the code to understand what datatype will a function return. To hint input types Example 1 def __init__ ( self , name : str , price : float , store_id : int ): self .name = name self .price = price self .store_id = store_id Example 2 @ classmethod def find_by_name ( cls , name : str ) -> "ItemModel" : # classmethod to search item in database by name return cls .query.filter_by( name =name).first() Example 3 @ classmethod def find_by_id ( cls , _id : int ) -> "UserModel" : # classmethod to search store in database by id return cls .query.filter_by( id =_id).first() adding ":str" or ":int" or ":float" in front of the parameter only tells other developers about what is the expected input. It does not actually participate in giving errors when user actually does that. To hint return types This requires to ...

Python - Numpy Lessons

import numpy as np The plus sign (+) used with a list works for concatenation. The plus sign (+) with numpy array works for vector addition. [1,2,3]+[2,3,4]=[1,2,3,2,3,4] np.array([1,2,3])+[2,3,4]=array([3,5,7]) Ways to do a dot product, i.e, multiply each element of an array with corresponding element of the other array and then adding up the results a = [2,3] b = [3,2] c = 0 WAYS: 1. for d, e in zip(a, b):     c += d*e 2. c = sum(a*b) 3. c = np.sum(a, b) 4. c = a.sum(b) 5. c = b.sum(a) 6. c = (a*b).sum() 7. c = np.dot(a, b) 8. c = a.dot(b) 9. c = b.dot(a) Finding magnitude, cos angle, and angle mag_a = np.linalg.norm(a) mag_b = np.linalg.norm(b) cos_angle = np.dot(a, b) / (mag_a * mag_b) angle = np.arccos(cos_angle)