Friday, 11 December 2020

list operations in python

  1. How to make each term of an array repeat, with the same terms grouped together?
    i/p:[A,B,C]
    o/p:[A,A,A,B,B,B,C,C,C]

    Solution:
    >>> l=['A','B','C']
    >>> [x for x in l for _ in range(3) ]
    ['A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C']

    [or]
    >>> import numpy as np
    >>> l=['A','B','C']
    >>> np.repeat(l,3).tolist()
    ['A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C']
    [or]

    >>> list(chain(*zip(*[l]*3)))
    ['A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C']
     



  2. adding 1 constant value to all sub_list in a list in python
    >>> l=[[1,2],[3,4],[5,6]]
    Add one more element('one') to the sublist
    Solution:
    >>> l=[[1,2],[3,4],[5,6]]
    >>> [x+['one'] for x in l]
    [[1, 2, 'one'], [3, 4, 'one'], [5, 6, 'one']]
  3. How do I remove all "nan" from the following list?
    mylist = [nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, n nan, 340000.0, 340050.0, 338003.0, 338004.0, 338005.0, 338006.0, 338008.0, 338010.0, 338011.0, 338018.0, 338019.0, nan, nan, nan, nan]
    Solution:
    
    mylist.remove(x) is a inplace operation and does return None always
    Instead you should be filtering it as follows
    >>> [x for x in mylist if not math.isnan(x)] [340000.0, 340050.0, 338003.0, 338004.0, 338005.0, 338006.0, 3380

Basic Python Quetions

Guido Van Rossum: Dutch programmer, popularly known as the author of Python programming language. He has created Python in 1989 and has worked for Google and Dropbox!

 why is it named Python?

While implementing Python, Van Rossum was also reading the published scripts from Monty Python’s Flying Circus, a BBC comedy series from the 1970s. Since he wanted a short, unique and slightly mysterious name for his invention, he got inspired by the series and named it Python!

Python is not that complex! It is a high-level dynamic programming language and is quite easy to learn. Python code has a very ‘natural’ style to it, which makes it easy to read and understand.
 The simplicity of Python is what makes it so popular!
  1. Free and open source
  2. Highly readable
  3. memory management
  4.  clean visual layout
  5. support oops
  6. High performance
  7.  

 

What are the key features of Python?

  • Python is an interpreted language. That means that, unlike languages like C and its variants, Python does not need to be compiled before it is run. Other interpreted languages include PHP and Ruby.
  • Python is dynamically typed, this means that you don’t need to state the types of variables when you declare them or anything like that. You can do things like x=111 and then x="I'm a string" without error
  • Python is well suited to object orientated programming in that it allows the definition of classes along with composition and inheritance. Python does not have access specifiers (like C++’s public, private), the justification for this point is given as “we are all adults here”
  • In Python, functions are first-class objects. This means that they can be assigned to variables, returned from other functions and passed into functions. Classes are also first class objects
  • Writing Python code is quick but running it is often slower than compiled languages. Fortunately,Python allows the inclusion of C based extensions so bottlenecks can be optimized away and often are. The numpy package is a good example of this, it’s really quite quick because a lot of the number crunching it does isn’t actually done by Python
  • Python finds use in many spheres – web applications, automation, scientific modelling, big data applications and many more. It’s also often used as “glue” code to get other languages and components to play nice.
2 How is memory managed in Python?
Ans: 
  1. Memory management in python is managed by Python private heap space. All Python objects and data structures are located in a private heap. The programmer does not have access to this private heap. The python interpreter takes care of this instead.
  2. The allocation of heap space for Python objects is done by Python’s memory manager. The core API gives access to some tools for the programmer to code.
  3. Python also has an inbuilt garbage collector, which recycles all the unused memory and so that it can be made available to the heap space
3. What is the usage of help() and dir() function in Python?
Ans: Help() and dir() both functions are accessible from the Python interpreter and used for viewing a consolidated dump of built-in functions. 
  1. Help() function: The help() function is used to display the documentation string and also facilitates you to see the help related to modules, keywords, attributes, etc.
  2. Dir() function: The dir() function is used to display the defined symbols.
4.

What does this mean: *args, **kwargs? And why would we use it?

Ans: We use *args when we aren’t sure how many arguments are going to be passed to a function, or if we want to pass a stored list or tuple of arguments to a function. **kwargsis used when we don’t know how many keyword arguments will be passed to a function, or it can be used to pass the values of a dictionary as keyword arguments. The identifiers args and kwargs are a convention, you could also use *bob and **billy but that would not be wise.

Tuesday, 8 December 2020

Regular Expression Exercise in python

  1. Write a Python program that matches a string that has an a followed by zero or more b's
    
    import re
    str_check=raw_input("Enter a string")
    match=re.search(r'ab*',str_check)
    if match:
        print("Matched string is",match.group())
    else:
        print("string didn't match")
    
    
    
    Enter a string abby
    ('Matched string is', 'abb')
    Enter a string a
    ('Matched string is', 'a')
    Enter a string b
    string didn't match
    

  2. Write a Python program that matches a string that has an a followed by one or more b's
    
    import re
    str_check=raw_input("Enter a string")
    match=re.search(r'ab+',str_check)
    if match:
        print("Matched string is",match.group())
    else:
        print("string didn't match")
    
    
    
    Enter a string abby
    ('Matched string is', 'abb')
    Enter a string a
    string did'nt match
    Enter a string b
    string didn't match
    
  3. Write a Python program that matches a string that has an a followed by zero or one 'b'
    
    import re
    str_check=raw_input("Enter a string")
    match=re.search(r'ab?',str_check)
    if match:
        print("Matched string is",match.group())
    else:
        print("string didn't match")
    
    
    
    Enter a string abbbbby
    ('Matched string is', 'ab')
    Enter a string a
    ('Matched string is', 'a')
    Enter a string b
    string didn't match
    
  4. Write a Python program that matches a string that has an a followed by three 'b'.
    
    import re
    str_check=raw_input("Enter a string")
    match=re.search(r'ab{3}',str_check)
    if match:
        print("Matched string is",match.group())
    else:
        print("string didn't match")
    
    
    
    Enter a string abbbby
    ('Matched string is', 'abbb')
    Enter a string abb
    string did'nt match
    Enter a string bbb
    string didn't match
    

  5. Write a Python program that matches a string that has an a followed by two to three 'b'
    
    import re
    str_check=raw_input("Enter a string")
    match=re.search(r'ab{2,3}',str_check)
    if match:
        print("Matched string is",match.group())
    else:
        print("string didn't match")
    
    
    
    Enter a string abby
    ('Matched string is', 'abb')
    Enter a string abbbbby
    ('Matched string is', 'abbb')
    Enter a string ab
    string didn't match
    

  6. Write a Python program to find sequences of lowercase letters joined with a underscore
    
    import re
    str_check=raw_input("Enter a string")
    match=re.search(r'[a-z]+_[a-z]+',str_check)
    if match:
        print("Matched string is",match.group())
    else:
        print("string didn't match")
    
    
    
    Enter a string alex_lion
    ('Matched string is', 'alex_lion')
    Enter a string abbbbby
    string didn't match
    

  7. Write a Python program to find sequences of one upper case letter followed by lower case letters
    
    import re
    str_check=raw_input("Enter a string")
    match=re.search(r'[A-Z]{1}[a-z]+',str_check)
    if match:
        print("Matched string is",match.group())
    else:
        print("string didn't match")
    
    
    
    Enter a string Alex_lion
    ('Matched string is', 'Alex')
    Enter a string alexTheLion
    ('Matched string is', 'The')
    

  8. Write a Python program that matches a string that has an 'a' followed by anything, ending in 'b'
    
    import re
    str_check=raw_input("Enter a string")
    match=re.search(r'a.+b$',str_check)
    if match:
        print("Matched string is",match.group())
    else:
        print("string didn't match")
    
    
    
    Enter a string theappleb
    ('Matched string is', 'appleb')
    Enter a string ab
    string didn't match
    

  9. Write a Python program that matches a word containing 'z', not start or end of the word

Saturday, 4 August 2018

Pandas exercise 1

>>>>import pandas as pd
>>>df = pd.DataFrame({"val": list(zip(range(9), range(9)[::-1]))})
>>>df 

use query to subset the df using the first entry(>3) of the tuple. 
      val
0  (0, 8)
1  (1, 7)
2  (2, 6)
3  (3, 5)
4  (4, 4)
5  (5, 3)
6  (6, 2)
7  (7, 1)
8  (8, 0)
Solution:
#Access the first elements of the tuple
>>> df.val.str[0]
0    0
1    1
2    2
3    3
4    4
5    5
6    6
7    7
8    8
>>> df.val.str[0].to_frame().query('val > 3')
   val
4    4
5    5
6    6
7    7
8    8
.str will work with any object column (this includes columns of lists and tuples), not just strings (strings are considered objects, one of many possible types).

Wednesday, 1 August 2018

list exercise in python

  1. Write a Python program to sum all the items in a list
  2. Write a Python program to multiplies all the items in a list
    >>>l
    [4, 5, 6, 7]
    >>> reduce(lambda x,y:x*y,l)
    840
  3. Write a Python program to get the largest number from a list
  4. Write a Python program to get the smallest number from a list
  5. Write a Python program to find the list of words that are longer than n from a given list of words.
  6. Write a Python program to count the number of strings where the string length is 2 or more and the first and last character are same from a given list of strings.
    >>> s=['12341','aaa','abc', 'xyz', 'aba', '1221','xx']
    >>> new_s=[x for x in s if len(x)>=2 and x[0]==x[-1]]
    >>> new_s
    ['12341', 'aaa', 'aba', '1221', 'xx']
     
    
    
      
  7. Write a Python program to get a list, sorted in increasing order by the last element in each tuple from a given list of non-empty tuples.
  8.  Sample List : [(2, 5), (1, 2), (4, 4), (2, 3), (2, 1)]
    Expected Result : [(2, 1), (1, 2), (2, 3), (4, 4), (2, 5)]
    >>> l= [(2, 5), (1, 2), (4, 4), (2, 3), (2, 1)]
    >>>l.sort(key=lambda x:x[1])
    >>>l
    [(2, 1), (1, 2), (2, 3), (4, 4), (2, 5)]
    [or]
    >>> import operator
    >>> l= [(2, 5), (1, 2), (4, 4), (2, 3), (2, 1)]
    >>> l.sort(key=operator.itemgetter(1))
    >>> l
    [(2, 1), (1, 2), (2, 3), (4, 4), (2, 5)]
  9. Write a Python program to remove duplicates from a list
  10. Write a Python program to check a list is empty or not
  11.  Write a Python program to remove all the elements from a list(empty the list).
  12. Write a proram to remove all occurences of a element(ex:5) from a list.
    >>> l=[2,3,4,2,3,4,5,6,7,5,4,2]
    >>> filter(lambda x:x!=5,l)
    [2, 3, 4, 2, 3, 4, 6, 7, 4, 2]
  13.  Write a Python program to clone or copy a list.
  14. Write a Python function that takes two lists and returns True if they have at least one common member.
  15. Write a Python program to print a specified list after removing the 0th, 4th and 5th elements.
    Sample List : ['Red', 'Green', 'White', 'Black', 'Pink', 'Yellow','Red']
    Expected Output : ['Green', 'White', 'Black','Red']
    >>> slist=['Red', 'Green', 'White', 'Black', 'Pink', 'Yellow','Red']
    >>> nlist=[e for i,e in enumerate(slist) if i not in [0,4,5]]
    >>> nlist
    ['Green', 'White', 'Black', 'Red']
  16. Write a Python program to generate a 3*4*6 3D array, whose each element is *
  17. Write a Python program to print the numbers of a specified list after removing even numbers from it
  18. Write a Python program to shuffle and print a specified list
    >>> l=[1,2,3,4,5,6,3,4,8,9,22,44,54,31,23]
    >>> random.shuffle(l)
    >>> l
    [23, 44, 4, 1, 5, 4, 54, 9, 22, 31, 6, 2, 3, 8, 3]
  19.  Write a Python program to generate and print a list of first and last 5 elements where the values are square of numbers between 1 and 30 (both included).
    >>> l=list(range(31))
    >>> l=[x*x for x in l[1:6]+l[-5:]]
    >>> l
    [1, 4, 9, 16, 25, 676, 729, 784, 841, 900]
  20. Write a Python program to generate and print a list except for the first 5 elements, where the values are square of numbers between 1 and 30 (both included).
    >>> l=list(range(31))
    >>> l=[x*x for x in l[5:-5]]
  21. Write a Python program to generate all permutations of a list in Python.
    >>> import itertools
    >>> l=[1,2,3,4]
    >>> list(itertools.permutations(l,2))
    [(1, 2), (1, 3), (1, 4), (2, 1), (2, 3), (2, 4), (3, 1), (3, 2), (3, 4), (4, 1), (4, 2), (4, 3)]
  22. Write a Python program to get the difference between the two lists
    >>> x=[1,2,3]
    >>> y=[4,5,6]
    >>> list(set(x)^set(y))
    [or]
    >>> list(set(x).symmetric_difference(y))
    [1, 2, 3, 4, 5, 6]
  23. Write a Python program access the index of a list. 
  24. Write a Python program to convert a list of characters into a string.
  25. Write a Python program to find the index of an item in a specified list.
  26. Write a Python program to flatten a shallow list.
    >>> l=[[1,2,3],[4,5],[7,8,9]]
    >>> from itertools import chain
    >>> list(chain(*l))
    [1, 2, 3, 4, 5, 7, 8, 9]
    [or]
    >>> l=[[1,2,3],[4,5],[7,8,9]]
    >>> sum(l,[])
    [1, 2, 3, 4, 5, 7, 8, 9]
  27. Write a Python program to append a list to the second list.
  28. Write a Python program to select an item randomly from a list.
    >>> l=list(range(31))
    >>> random.choice(l)
    13
  29. Write a python program to check whether two lists are circularly identical.
  30. Write a Python program to get unique values from a list.
    >>> l=[1,2,3,4,5,3,4,8]
    >>> list(set(l))
    [1, 2, 3, 4, 5, 8]
  31. Write a Python program to find the second smallest number in a list.
  32. Write a Python program to find the second largest number in a list.
  33. Write a Python program to get the frequency of the elements in a list
    >>> l=[1,2,3,4,5,3,4,8]
    >>> {x:l.count(x) for x in l}
    {1: 1, 2: 1, 3: 2, 4: 2, 5: 1, 8: 1}
  34. Write a Python program to count the number of elements in a list within a specified range.
    >>> x=[10,23,21,45,65,34,25,78,54,43,89]
    >>> len([i for i in x if i>=30 and i<=60])
    4
  35. Write a Python program to check whether a list contains a sublist.
  36. Write a Python program to generate all sublists of a list


Monday, 30 July 2018

pandas-programming practice 1

1.How to create a Boolean column that compares the value of the next n rows with the actual value of a row

Input
A  B  C
14 3 32
28 3 78
15 4 68
42 3 42
24 4 87
13 3 65

Calculation for D: if any of the next n rows (in this case 3) have a value that is >= than the actual row (n)+30 then return 1, else 0

OUTPUT
A  B  C  D
14 3 32  1     # 32+30 = 62 so [78>=62, 68>=62]
28 3 78  0     # 78+30 = 108 
15 4 68  0     # 68+30 = 98
42 3 42  1     # 42+30 = 72 so [87>=72]  
24 4 87  0     # 87+30 = 117
13 3 65  0     # 65+30 = 95
 
Solution:
>>> import pandas as pd
>>> import numpy as np
>>> df=pd.read_csv("pan1.csv")
>>> df['D'] = np.where((df.C+30<=df.C.shift(-1)) | ((df.C+30<=df.C.shift(-2))),1,0)
 
[or]
>>> import pandas as pd
>>> df=pd.read_csv("pan1.csv")  
>>> df['D'] =df.C.iloc[::-1].rolling(3,min_periods=1).max().iloc[::-1].gt(df.C+30).astype(int)

Saturday, 28 July 2018

List comprehension

  1. Iterating through a string Using List Comprehension

    l = [ x for x in 'abcde' ]
    print( l)
    When we run the program, the output will be:
    ['a', 'b', 'c', 'd', 'e']
  2. Using if with List Comprehension

    l=[x for x in range(25) if x%5==0]
    print(l)
  3. [0, 5, 10, 15, 20]
  4. Nested IF with List Comprehension

    l=[x for x in range(50) if x%5==0 if x%2==0]
     
    Here, list comprehension checks:
    
        Is x divisible by 5 or not
        Is x divisible by 2 or not
    
    If x satisfies both conditions, x is appended to list l.
    
  5. if...else With List Comprehension

    l=["Even" if x%2==0 else "odd" for x in range(10)]
    print(l)
    ['Even', 'odd', 'Even', 'odd', 'Even', 'odd', 'Even', 'odd', 'Even', 'odd']

  6. Nested Loops in List Comprehension

    prefix = ['A', 'B', 'C']
    suffix = ['a', 'b']
    result = [val+" "+val2 for val in prefix for val2 in suffix ]
    print(result)
    o/p
    ['A a', 'A b', 'B a', 'B b', 'C a', 'C b']
    
    
    
    
     

Python exercises

  1. a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]. Write one line of Python that takes this list a and makes a new list that has only the even elements of this list in it.
  2. given a list,years_of_birth = [1990, 1991, 1990, 1990, 1992, 1991]
    find the list of age?
  3. Find all of the numbers from 1-1000 that have a 3 in them? 
  4. Count the number of spaces in a string
  5. Remove all of the vowels in a string
  6. Find all of the words in a string that are less than 4 letter.
  7. A list of all consonants in the sentence 'The quick brown fox jumped over the lazy dog' 
  8. A list of all the capital letters (and not white space) in 'The Quick Brown Fox Jumped Over The Lazy Dog'
  9. A list of all square numbers formed by squaring the numbers from 1 to 1000.
  10. Create the list:

    [-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0] 
  11.     return a list of the full path to items in a directory (hint, use os.listdir() and os.path.join())
  12. now extend the exercise11 to exclude directories (hint, use os.path.isdir())
  13. now extend exercise11  further to filter to only files ending in .jpg and .png
  14. Create a sample list of random integers with list comprehension and the random module. (2 lines max, import included) 
  15. Create a 5 x 5 matrix using a list of lists 
    nest_li = [[0, 1, 2, 3, 4],
              [0, 1, 2, 3, 4],
              [0, 1, 2, 3, 4],
              [0, 1, 2, 3, 4],
              [0, 1, 2, 3, 4]]
    Hint:[[output expression] for iterator variable in iterable]
    
    Note that here, the output expression is itself a list comprehension. 

Challenge:

  1. Use a dictionary comprehension to count the length of each word in a sentence.
  2. Use a dictionary comprehension to build a map of every ascii character to its ordinal number chr : ord and update it with the reverse mapping ord : chr. Obvisouly the chr and ord functions and the string module will help. (3 lines max, import included)
  3. Use a nested list comprehension to find all of the numbers from 1-1000 that are divisible by any single digit besides 1 (2-9)

  4. For all the numbers 1-1000, use a nested list/dictionary comprehension to find the highest single digit any of the numbers is divisible by.
  5. Convert a list of dicts which you have to create with a list comprehension, all with an unique id to a dict of dicts with that id as key. (2 lines, one for the list comp, one for the dict comp)

list operations in python

How to make each term of an array repeat, with the same terms grouped together? i/p: [A,B,C] o/p: [A,A,A,B,B,B,C,C,C] Solution: >...