Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Wednesday 13 March 2019

Website Scraping with Python: Using BeautifulSoup and Scrapy Kindle Edition by Gábor László Hajba

Closely examine website scraping and data processing: the technique of extracting data from websites in a format suitable for further analysis. You'll review which tools to use, and compare their features and efficiency. Focusing on BeautifulSoup4 and Scrapy, this concise, focused book highlights common problems and suggests solutions that readers can implement on their own.

Website Scraping with Python starts by introducing and installing the scraping tools and explaining the features of the full application that readers will build throughout the book. You'll see how to use BeautifulSoup4 and Scrapy individually or together to achieve the desired results. Because many sites use JavaScript, you'll also employ Selenium with a browser emulator to render these sites and make them ready for scraping.

By the end of this book, you'll have a complete scraping application to use and rewrite to suit your needs. As a bonus, the author shows you options of how to deploy your spiders into the Cloud to leverage your computer from long-running scraping tasks.

What You'll Learn
  • Install and implement scraping tools individually and together
  • Run spiders to crawl websites for data from the cloud
  • Work with emulators and drivers to extract data from scripted sites

Who This Book Is For

Readers with some previous Python and software development experience, and an interest in website scraping.
Buy :
 
PDF Download :


Deep Learning with Python Paperback – Import, 30 Nov 2017 by Francois Chollet

Deep learning is applicable to a widening range of artificial
intelligence problems, such as image classification, speech recognition,
text classification, question answering, text-to-speech, and optical
character recognition.

Deep Learning with Python is structured around a series of practical
code examples that illustrate each new concept introduced and
demonstrate best practices. By the time you reach the end of this book,
you will have become a Keras expert and will be able to apply deep
learning in your own projects.

KEY FEATURES
• Practical code examples
• In-depth introduction to Keras
• Teaches the difference between Deep Learning and AI

ABOUT THE TECHNOLOGY
Deep learning is the technology behind photo tagging systems at
Facebook and Google, self-driving cars, speech recognition systems on
your smartphone, and much more.

AUTHOR BIO
Francois Chollet is the author of Keras, one of the most widely used
libraries for deep learning in Python. He has been working with deep neural
networks since 2012. Francois is currently doing deep learning research at
Google. He blogs about deep learning at blog.keras.io.

Buy :


PDF Download :



Tuesday 12 March 2019

Working with Dictionary in Python

Python Dictionaries

A dictionary is a set of unordered key, value pairs. In a dictionary, the keys must be unique and they are stored in an unordered manner.
In this blog you will learn the basics of how to use the Python dictionary.

By the end of the tutorial you will be able to - Create Dictionaries - Get values in a Dictionary - Add and delete elements in a Dictionary - To and For Loops in a Dictionary
Creating a Dictionary

Let’s try to build a profile of three people using dictionaries. To do that you separate the key-value pairs by a colon(“:”). The keys would need to be of an immutable type, i.e., data-types for which the keys cannot be changed at runtime such as int, string, tuple, etc. The values can be of any type. Individual pairs will be separated by a comma(“,”) and the whole thing will be enclosed in curly braces({...}).

For example, you can have the fields “city”, “name,” and “food” for keys in a dictionary and assign the key, value pairs to the dictionary variable person1_information.

>>> person_information = {'city': 'San Francisco', 'name': 'Sam', "food": "shrimps"}
>>> type(person1_information)
>>> print(person1_information)
{'city': 'San Francisco', 'name': 'Sam', 'food': 'shrimps'}
Get the values in a Dictionary

To get the values of a dictionary from the keys, you can directly reference the keys. To do this, you enclose the key in brackets [...] after writing the variable name of the dictionary.
So, in the following example, a dictionary is initialized with keys “city”, “name,” and “food” and you can retrieve the value corresponding to the key “city.”

>>> create a dictionary person1_information
>>> person1_information = {'city': 'San Francisco', 'name': 'Sam', "food":"shrimps"}
>>> print the dictionary
>>> print(person1_information["city"])
San Francisco

You can also use the get method to retrieve the values in a dict. The only difference is that in the get method, you can set a default value. In direct referencing, if the key is not present, the interpreter throws KeyError.

>>> # create a small dictionary
>>> alphabets = {1: ‘a’}
>>> # get the value with key 1
>>> print(alphabets.get(1))
'a'
>>> # get the value with key 2. Pass “default” as default. Since key 2 does not exist, you get “default” as the return value.
>>> print(alphabets.get(2, "default"))
'default'
>>> # get the value with key 2 through direct referencing
>>> print(alphabets[2])
Traceback (most recent call last):
File "stdin", line 1, in module KeyError: 2
Looping over dictionary

Say, you got a dictionary, and you want to print the keys and values in it. Note that the key-words for and in are used which are used when you try to loop over something. To learn more about looping please look into tutorial on looping.
>>> person1_information = {'city': 'San Francisco', 'name': 'Sam', "food": "shrimps"}
>>> for k, v in person1_information.items():
... print("key is: %s" % k)
... print("value is: %s" % v)
... print("###########################")
... key is: food
value is: shrimps
###########################
key is: city
value is: San Francisco
###########################
key is: name
value is: Sam
###########################
Add elements to a dictionary
You can add elements by updating the dictionary with a new key and then assigning the value to a new key.
>>> # initialize an empty dictionary
>>> person1_information = {}
>>> # add the key, value information with key “city”
>>> person1_information["city"] = "San Francisco"
>>> # print the present person1_information
>>> print(person1_information)
{'city': 'San Francisco'}
>>> # add another key, value information with key “name”
>>> person1_information["name"] = "Sam"
>>> # print the present dictionary
>>> print(person1_information)
{'city': 'San Francisco', 'name': 'Sam'}
>>> # add another key, value information with key “food”
>>> person1_information["food"] = "shrimps"
>>> # print the present dictionary
>>> print(person1_information)
{'city': 'San Francisco', 'name': 'Sam', 'food': 'shrimps'}
Or you can combine two dictionaries to get a larger dictionary using the update method.
>>> # create a small dictionary
>>> person1_information = {'city': 'San Francisco'}
>>> # print it and check the present elements in the dictionary
>>> print(person1_information)
{'city': 'San Francisco'}
>>> # have a different dictionary
>>> remaining_information = {'name': 'Sam', "food": "shrimps"}
>>> # add the second dictionary remaining_information to personal1_information using the update method
>>> person1_information.update(remaining_information)
>>> # print the current dictionary
>>> print(person1_information)
{'city': 'San Francisco', 'name': 'Sam', 'food': 'shrimps'}
Delete Elements in a Dicitionary
To delete a key, value pair in a dictionary, you can use the del method.
>>> # initialise a dictionary with the keys “city”, “name”, “food”
>>> person1_information = {'city': 'San Francisco', 'name': 'Sam', "food": "shrimps"}
>>> # delete the key, value pair with the key “food”
>>> del person1_information["food"]
>>> # print the present personal1_information. Note that the key, value pair “food”: “shrimps” is not there anymore.
>>> print(person1_information)
{'city': 'San Francisco', 'name': 'Sam'}
A disadvantage is that it gives KeyError if you try to delete a nonexistent key.
>>> # initialise a dictionary with the keys “city”, “name”, “food”
>>> person1_information = {'city': 'San Francisco', 'name': 'Sam', "food": "shrimps"}
>>> # deleting a non existent key gives key error.
>>> del person1_information["non_existent_key"]
Traceback (most recent call last):
File "", line 1, in
KeyError: 'non_existent_key'

So, instead of the del statement you can use the pop method. This method takes in the key as the parameter. As a second argument, you can pass the default value if the key is not present.

>>> # initialise a dictionary with key, value pairs
>>> person1_information = {'city': 'San Francisco', 'name': 'Sam', "food": "shrimps"}
>>> # remove a key, value pair with key “food” and default value None
>>> print(person1_information.pop("food", None))
'Shrimps'
>>> # print the updated dictionary. Note that the key “food” is not present anymore
>>> print(person1_information)
{'city': 'San Francisco', 'name': 'Sam'}
>>> # try to delete a nonexistent key. This will return None as None is given as the default value.
>>> print(person1_information.pop("food", None))
None

Saturday 9 March 2019

Working with Lists in Python

lists
A list is a data-structure, or it can be considered a container that can be used to store multiple data at once. The list will be ordered and there will be a definite count of it. The elements are indexed according to a sequence and the indexing is done with 0 as the first index. Each element will have a distinct place in the sequence and if the same value occurs multiple times in the sequence, each will be considered separate and distinct element. A more detailed description on lists and associated data-types are covered in this blog.

In this blog you will come to know of the about how to create python lists and the common paradigms for a python list. Lists are great if you want to preserve the sequence of the data and then iterate over them later for various purposes. We will cover iterations and for loops in our tutorials on for loops.

How to create a list:-

To create a list, you separate the elements with a comma and enclose them with a bracket “[]”. For example, you can create a list of company names containing “hackerearth”, “google”, “facebook”. This will preserve the order of the names.

>>> companies = ["hackerearth", "google", "facebook"] >>> # get the first company name >>> print(companies[0]) 'hackerearth' >>> # get the second company name >>> print(companies[1]) 'google' >>> # get the third company name >>> print(companies[2]) 'facebook' >>> # try to get the fourth company name >>> # but this will return an error as only three names >>> # have been defined. >>> print(companies[3]) Traceback (most recent call last): File "stdin", line 1, in module IndexError: list index out of range

Trying to access elements outside the range will give an error. You can create a two-dimensional list. This is done by nesting a list inside another list. For example, you can group “hackerearth” and “paytm” into one list and “tcs” and “cts” into another and group both the lists into another “master” list.

>>> companies = [["hackerearth", "paytm"], ["tcs", "cts"]] >>> print(companies) [['hackerearth', 'paytm'], ['tcs', 'cts']]

Methods over Python Lists

Python lists support common methods that are commonly required while working with lists. The methods change the list in place. (More on methods in the classes and objects tutorial). In case you want to make some changes in the list and keep both the old list and the changed list, take a look at the functions that are described after the methods.

How to add elements to the list:

1.list.append(elem) - will add another element to the list at the end.
>>> # create an empty list >>> companies = [] >>> # add “hackerearth” to companies >>> companies.append(“hackerearth”) >>> # add "google" to companies >>> companies.append("google") >>> # add "facebook" to companies >>> companies.append("facebook") >>> # print the items stored in companies >>> print(companies) ['hackerearth', 'google', 'facebook']

Note the items are printed in the order in which they youre inserted. 2.list.insert(index, element) - will add another element to the list at the given index, shifting the elements greater than the index one step to the right. In other words, the elements with the index greater than the provided index will increase by one. For example, you can create a list of companies ['hackerearth', 'google', 'facebook'] and insert “airbnb” in third position which is held by “facebook”.

>>> # initialise a preliminary list of companies >>> companies = ['hackerearth', 'google', 'facebook'] >>> # check what is there in position 2 >>> print(companies[2]) facebook >>> # insert “airbnb” at position 2 >>> companies.insert(2, "airbnb") >>> # print the new companies list >>> print(companies) ['hackerearth', 'google', 'airbnb', 'facebook'] >>> # print the company name at position 2 >>> print(companies[2]) airbnb

3.list.extend(another_list) - will add the elements in list 2 at the end of list

For example, you can concatenate two lists ["haskell", "clojure", "apl"] and ["scala", "F#"] to the same list langs. >>> langs = ["haskell", "clojure", "apl"] >>> langs.extend(["scala", "F#"]) >>> print(langs) ['haskell', 'clojure', 'apl', 'scala', 'F#'] 3.list.index(elem) - will give the index number of the element in the list. For example, if you have a list of languages with elements ['haskell', 'clojure', 'apl', 'scala', 'F#'] and you want the index of “scala”, you can use the index method. >>> index_of_scala = langs.index("scala") >>> print(index_of_scala) 3

How to remove elements from the list:

1. list.remove(elem) - will search for the first occurrence of the element in the list and will then remove it. For example, if you have a list of languages with elements ['haskell', 'clojure', 'apl', 'scala', 'F#'] and you want to remove scala, you can use the remove method.
>>> langs.remove("scala") >>> print(langs) ['haskell', 'clojure', 'apl', 'F#']
2. list.pop() - will remove the last element of the list. If the index is provided, then it will remove the element at the particular index. For example, if you have a list [5, 4, 3, 1] and you apply the method pop, it will return the last element 1 and the resulting list will not have it.
>>> # assign a list to some_numbers >>> some_numbers = [5, 4, 3, 1] >>> # pop the list >>> some_numbers.pop() 1 >>> # print the present list >>> print(some_numbers) [5, 4, 3] Similarly, try to pop an element from a random index that exists in the list. >>> # pop the element at index 1 >>> some_numbers.pop(1) 4 >>> # check the present list >>> print(some_numbers) [5, 3]

Other useful list methods

1.list.sort() - will sort the list in-place. For example, if you have an unsorted list [4,3,5,1], you can sort it using the sort method.
>>> # initialise an unsorted list some_numbers >>> some_numbers = [4,3,5,1] >>> # sort the list >>> some_numbers.sort() >>> # print the list to see if it is sorted. >>> some_numbers [1, 3, 4, 5]

2.list.reverse() - will reverse the list in place For example, if you have a list [1, 3, 4, 5] and you need to reverse it, you can call the reverse method.
>>> # initialise a list of numbers that >>> some_numbers = [1, 3, 4, 5] >>> # Try to reverse the list now >>> some_numbers.reverse() >>> # print the list to check if it is really reversed. >>> print(some_numbers) [5, 4, 3, 1]

Functions over Python Lists:
1.You use the function “len” to get the length of the list. For example, if you have a list of companies ['hackerearth', 'google', 'facebook'] and you want the list length, you can use the len function.

>>> # you have a list of companies >>> companies = ['hackerearth', 'google', 'facebook'] >>> # you want the length of the list >>> print(len(companies)) 3

2. If you use another function “enumerate” over a list, it gives us a nice construct to get both the index and the value of the element in the list. For example, you have the list of companies ['hackerearth', 'google', 'facebook'] and you want the index, along with the items in the list, you can use the enumerate function.
>>> # loop over the companies and print both the index as youll as the name. >>> for indx, name in enumerate(companies): ... print("Index is %s for company: %s" % (indx, name)) ... Index is 0 for company: hackerearth Index is 1 for company: google Index is 2 for company: facebook
In this example, you use the for loop. For loops are pretty common in all programming languages that support procedural constructs.
3. sorted function will sort over the list Similar to the sort method, you can also use the sorted function which also sorts the list. The difference is that it returns the sorted list, while the sort method sorts the list in place. So this function can be used when you want to preserve the original list as well.
>>> # initialise a list >>> some_numbers = [4,3,5,1] >>> # get the sorted list >>> print(sorted(some_numbers)) [1, 3, 4, 5] >>> # the original list remains unchanged >>> print(some_numbers) [4, 3, 5, 1]

Wednesday 6 March 2019

Python Closures

Before seeing what a closure is, we have to first understand what are nested functions and non-local variables.

Nested functions in Python
A function which is defined inside another function is known as nested function. Nested functions are able to access variables of the enclosing scope.

In Python, these non-local variables can be accessed only within their scope and not outside their scope. This can be illustrated by following example:

# Python program to illustrate
# nested functions
def outerFunction(text):
text = text
def innerFunction():
print(text)
innerFunction()
if __name__ == '__main__':
outerFunction('Hey!')

As we can see innerFunction() can easily be accessed inside the outerFunction body but not outside of it’s body. Hence, here, innerFunction() is treated as nested Function which uses text as non-local variable.

Python Closures
A Closure is a function object that remembers values in enclosing scopes even if they are not present in memory.

1.It is a record that stores a function together with an environment: a mapping associating each free variable of the function (variables that are used locally, but defined in an enclosing scope) with the value or reference to which the name was bound when the closure was created.

2.A closure—unlike a plain function—allows the function to access those captured variables through the closure’s copies of their values or references, even when the function is invoked outside their scope.

# Python program to illustrate
# closures
def outerFunction(text):
text = text
def innerFunction():
print(text)
return innerFunction # Note we are returning function WITHOUT parenthesis
if __name__ == '__main__':
myFunction = outerFunction('Hey!')
myFunction()

Output:
Hey!
1.As observed from above code, closures help to invoke function outside their scope.
2.The function innerFunction has its scope only inside the outerFunction. But with the use of closures we can easily extend its scope to invoke a function outside its scope

# Python program to illustrate
# closures
import logging
logging.basicConfig(filename='example.log', level=logging.INFO)
def logger(func):
def log_func(*args):
logging.info('Running "{}" with arguments {}'.format(func.__name__, args))
# Necessary for closure to work (returning WITHOUT parenthesis)
return log_func
def add(x, y):
return x+y
def sub(x, y):
return x-y
add_logger = logger(add)
sub_logger = logger(sub)
add_logger(3, 3)
add_logger(4, 5)
sub_logger(10, 5)
sub_logger(20, 10)

Output:
6
9
5
10

When and why to use Closures:
1.As closures are used as callback functions, they provide some sort of data hiding. This helps us to reduce the use of global variables. 2.When we have few functions in our code, closures prove to be efficient way. But if we need to have many functions, then go for class (OOP).

Precision Handling in Python

Precision Handling in Python
Python in its definition allows to handle precision of floating point numbers in several ways using different functions. Most of them are defined under the “math” module. Some of the most used operations are discussed in this article.

1. trunc() :- This function is used to eliminate all decimal part of the floating point number and return the integer without the decimal part.
2. ceil() :- This function is used to print the least integer greater than the given number.
3. floor() :- This function is used to print the greatest integer smaller than the given integer.

# Python code to demonstrate ceil(), trunc()
# and floor()
# importing "math" for precision function
import math
# initializing value
a = 3.4536
# using trunc() to print integer after truncating
print ("The integral value of number is : ",end="")
print (math.trunc(a))
# using ceil() to print number after ceiling
print ("The smallest integer greater than number is : ",end="")
print (math.ceil(a))
# using floor() to print number after flooring
print ("The greatest integer smaller than number is : ",end="")
print (math.floor(a))

Output:
The integral value of number is : 3
The smallest integer greater than number is : 4
The greatest integer smaller than number is : 3

Setting Precision

There are many ways to set precision of floating point value. Some of them is discussed below.
1. Using “%” :- “%” operator is used to format as well as set precision in python. This is similar to “printf” statement in C programming.
2. Using format() :- This is yet another way to format the string for setting precision.
3. Using round(x,n) :- This function takes 2 arguments, number and the number till which we want decimal part rounded.
# Python code to demonstrate precision
# and round()
# initializing value
a = 3.4536
# using "%" to print value till 2 decimal places
print ("The value of number till 2 decimal place(using %) is : ",end=" ")
print ('%.2f'%a)
# using format() to print value till 2 decimal places
print ("The value of number till 2 decimal place(using format()) is : ",end=" ")
print ("{0:.2f}".format(a))
# using round() to print value till 2 decimal places
print ("The value of number till 2 decimal place(using round()) is : ",end=" ")
print (round(a,2))

Output:
The value of number till 2 decimal place(using %) is : 3.45
The value of number till 2 decimal place(using format()) is : 3.45
The value of number till 2 decimal place(using round()) is : 3.45

Tuesday 5 March 2019

Yield Instead of return in Python

When to use yield instead of return in Python?

The yield statement suspends function’s execution and sends a value back to caller, but retains enough state to enable function to resume where it is left off. When resumed, the function continues execution immediately after the last yield run. This allows its code to produce a series of values over time, rather them computing them at once and sending them back like a list.
Let’s see with an example:

# A Simple Python program to demonstrate working
# of yield
# A generator function that yields 1 for first time,
# 2 second time and 3 third time
def simpleGeneratorFun():
yield 1
yield 2
yield 3

# Driver code to check above generator function
for value in simpleGeneratorFun():
print(value)

Output:
1
2
3

Return sends a specified value back to its caller whereas Yield can produce a sequence of values. We should use yield when we want to iterate over a sequence, but don’t want to store the entire sequence in memory.

Yield are used in Python generators. A generator function is defined like a normal function, but whenever it needs to generate a value, it does so with the yield keyword rather than return. If the body of a def contains yield, the function automatically becomes a generator function.

# A Python program to generate squares from 1
# to 100 using yield and therefore generator
# An infinite generator function that prints
# next square number. It starts with 1
def nextSquare():
i = 1;
# An Infinite loop to generate squares
while True:
yield i*i
i += 1 # Next execution resumes
# from this point
# Driver code to test above generator
# function
for num in nextSquare():
if num > 100:
break
print(num)

Output:
1
4
9
16
25
36
49
64
81
100

Empty Function in Python

In C/C++ and Java, we can write empty function as following
// An empty function in C/C++/Java

void fun() { }
In Python, if we write something like following in Python, it would produce compiler error.
# Incorrect empty function in Python
def fun():

Output:
IndentationError: expected an indented block
In Python, to write empty functions, we use pass statement. pass is a special statement in Python that does nothing. It only works as a dummy statement.

# Correct way of writing empty function
# in Python
def fun():
pass

We can use pass in empty while statement also.
# Empty loop in Python
mutex = True
while (mutex == True) :
pass

We can use pass in empty if else statements.
# Empty in if/else in Python
mutex = True
if (mutex == True) :
pass
else :
print("False")

Monday 4 March 2019

Function Decorator in Python

Following are important facts about functions in Python that are useful to understand decorator functions.

1.In Python, we can define a function inside another function.
2.In Python, a function can be passed as parameter to another function (a function can also return another function).

# A Python program to demonstrate that a function
# can be defined inside another function and a
# function can be passed as parameter.
# Adds a welcome message to the string
def messageWithWelcome(str):
# Nested function
def addWelcome():
return "Welcome to "
# Return concatenation of addWelcome()
# and str.
return addWelcome() + str
# To get site name to which welcome is added
def site(site_name):
return site_name
print messageWithWelcome(site("PythonWorld"))

Output:
Welcome to PythonWorld

Function Decorator
A decorator is a function that takes a function as its only parameter and returns a function. This is helpful to “wrap” functionality with the same code over and over again. For example, above code can be re-written as following.
We use @func_name to specify a decorator to be applied on another function.
# Adds a welcome message to the string
# returned by fun(). Takes fun() as
# parameter and returns welcome().
def decorate_message(fun):
# Nested function def addWelcome(site_name):
return "Welcome to " + fun(site_name)
# Decorator returns a function
return addWelcome
@decorate_message
def site(site_name):
return site_name;
# Driver code # This call is equivalent to call to # decorate_message() with function # site("GeeksforGeeks") as parameter print site("PythonWorld")

Output:
Welcome to PythonWorld

Iterators in Python

Iterator in python is any python type that can be used with a ‘for in loop’. Python lists, tuples, dicts and sets are all examples of inbuilt iterators. These types are iterators because they implement following methods. In fact, any object that wants to be an iterator must implement following methods.

1.__iter__ method that is called on initialization of an iterator. This should return an object that has a next or __next__ (in Python 3) method.
2.next ( __next__ in Python 3) The iterator next method should return the next value for the iterable. When an iterator is used with a ‘for in’ loop, the for loop implicitly calls next() on the iterator object. This method should raise a StopIteration to signal the end of the iteration.

Below is a simple Python program that creates iterator type that iterates from 10 to given limit. For example, if limit is 15, then it prints 10 11 12 13 14 15. And if limit is 5, then it prints nothing.

# A simple Python program to demonstrate
# working of iterators using an example type
# that iterates from 10 to given value
# An iterable user defined type
class Test:
# Cosntructor
def __init__(self, limit):
self.limit = limit
# Called when iteration is initialized
def __iter__(self):
self.x = 10
return self
# To move to next element. In Python 3,
# we should replace next with __next__
def next(self):
# Store current value of x
x = self.x
# Stop iteration if limit is reached
if x > self.limit:
raise StopIteration
# Else increment and return old value
self.x = x + 1;
return x
# Prints numbers from 10 to 15
for i in Test(15):
print(i)
# Prints nothing
for i in Test(5):
print(i)

Output:
10
11
12
13
14
15

Accessing Counters in Python

Once initialized, counters are accessed just like dictionaries. Also, it does not raise the Key Value error (if key is not present) instead the value’s count is shown as 0.

Example:
# Python program to demonstrate accessing of
# Counter elements
from collections import Counter
# Create a list
z = ['blue', 'red', 'blue', 'yellow', 'blue', 'red']
col_count = Counter(z)
print(col_count)
col = ['blue','red','yellow','green']
# Here green is not in col_count
# so count of green will be zero
for color in col:
print (color, col_count[color])

Output:
Counter({'blue': 3, 'red': 2, 'yellow': 1})
blue 3
red 2
yellow 1
green 0

elements()
The elements() method returns an iterator that produces all of the items known to the Counter. Note : Elements with count <= 0 are not included.

Example:
# Python example to demonstrate elements() on
# Counter (gives back list)
from collections import Counter
coun = Counter(a=1, b=2, c=3)
print(coun)
print(list(coun.elements()))

Output:
Counter({'c': 3, 'b': 2, 'a': 1})
['a', 'b', 'b', 'c', 'c', 'c']

most_common() :
most_common() is used to produce a sequence of the n most frequently encountered input values and their respective counts.
# Python example to demonstrate most_elements() on
# Counter
from collections import Counter
coun = Counter(a=1, b=2, c=3, d=120, e=1, f=219)
# This prints 3 most frequent characters
for letter, count in coun.most_common(3):
print('%s: %d' % (letter, count))

Output:
f: 219
d: 120
c: 3

Counter in Python

What is counter?
Counter is a container included in the collections module.

What is container?
Containers are objects that hold objects. They provide a way to access the contained objects and iterate over them. Examples of built in containers are Tuple, list and dictionary. Others are included in Collections module.
A Counter is a subclass of dict. Therefore it is an unordered collection where elements and their respective count are stored as dictionary. This is equivalent to bag or multiset of other languages.

Syntax:
class collections.Counter([iterable-or-mapping])
initialization
The constructor of counter can be called in any one of the following ways :
1.With sequence of items
2.With dictionary containing keys and counts
3.With keyword arguments mapping string names to counts
Example of each type of initialization :
# A Python program to show different ways to create
# Counter
from collections import Counter
# With sequence of items
print Counter(['B','B','A','B','C','A','B','B','A','C'])
# with dictionary
print Counter({'A':3, 'B':5, 'C':2})
# with keyword arguments
print Counter(A=3, B=5, C=2)
Output of all the three lines is same :
Counter({'B': 5, 'A': 3, 'C': 2})
Counter({'B': 5, 'A': 3, 'C': 2})
Counter({'B': 5, 'A': 3, 'C': 2})

Updation:
We can also create an empty counter in the following manner :
coun = collections.Counter()
And can be updated via update() method .Syntax for the same :
coun.update(Data)
# A Python program to demonstrate update()
from collections import Counter
coun = Counter()
coun.update([1, 2, 3, 1, 2, 1, 1, 2])
print(coun)
coun.update([1, 2, 4])
print(coun)

Output:
Counter({1: 4, 2: 3, 3: 1})
Counter({1: 5, 2: 4, 3: 1, 4: 1})

Popular Posts

Categories

AI (27) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (113) C (77) C# (12) C++ (82) Course (60) Coursera (176) coursewra (1) Cybersecurity (22) data management (11) Data Science (85) Data Strucures (6) Deep Learning (9) Django (6) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (5) flutter (1) FPL (17) Google (18) Hadoop (3) HTML&CSS (46) IBM (25) IoT (1) IS (25) Java (92) Leet Code (4) Machine Learning (43) Meta (18) MICHIGAN (4) microsoft (3) Pandas (3) PHP (20) Projects (29) Python (726) Python Coding Challenge (170) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (40) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses