- 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']
- 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']] - 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 returnNone
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
Friday 11 December 2020
list operations in python
Basic Python Quetions
Guido Van Rossum: A 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!- Free and open source
- Highly readable
- memory management
- clean visual layout
- support oops
- High performance
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 thenx="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.
Ans:
- 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.
- 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.
- 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
Ans: Help()
and dir() both functions are accessible from the Python interpreter and
used for viewing a consolidated dump of built-in functions.
- 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.
- Dir() function: The dir() function is used to display the defined symbols.
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. **kwargs
is 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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')
- 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
- Write a Python program that matches a word containing 'z', not start or end of the word
Subscribe to:
Posts (Atom)
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: >...
-
In the Gregorian calendar three criteria must be taken into account to identify leap years: The year can be evenly divided by 4, is a le...
-
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: >...
-
Guido Van Rossum: A Dutch programmer, popularly known as the author of Python programming language. He has created Python in 1989 an...