Change language

# Checking an anagram in Python using collection.Counter ()

| | |

Examples:

` Input: str1 = “abcd”, str2 = “dabc” Output: True Input: str1 = “abcf”, str2 = “kabc” Output: False `

This problem has an existing solution, please refer.  collection.Counter () module .

 ` # Python code to check for two lines ` ` # anagram ` ` from ` ` collections ` ` import ` ` Counter `   ` def ` ` anagram (input1, input2): `   ` ` ` # Counter () returns dictionary data ` ` ` ` # structure that contains characters ` ` ` ` # input as key and their frequency ` ` # as appropriate value ` ` return ` ` Counter (input1) ` ` = ` ` = ` ` Counter (input2) `   ` # Driver function ` ` if ` ` __ name__ ` ` = ` ` = ` ` "__ main__" ` `: ` ` input1 ` ` = ` ` ’abcd’ ` ` input2 ` ` = ` ` ’dcab’ ` ` ` ` print ` ` anagram (input1, input2) `

Output:

` True `

How does dictionary comparison work in python?
If we have two dictionary data structures in python dict1 = {& # 39; a & # 39 ;: 2, & # 39; b & # 39 ;: 3, & # 39; c & # 39 ;: 1} and dict2 = {& # 39; b & # 39 ;: 3, & # 39; c & # 39 ;: 1, & # 39; a & # 39 ;: 2} and we compare them both as dict1 = dict2, then it will be True . In Python, a normal dictionary data structure does not follow the order of the keys, when we compare two dictionaries, then three checks are compared in order of key numbers (if they do not match, the values ​​do not match) , key names ( if they do not match, they are not equal) and the value of each key (they must also be & # 39; == & # 39;) .

This article provided by Shashank Mishra (Gullu) . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

## Shop

Learn programming in R: courses

\$

Best Python online courses for 2022

\$

Best laptop for Fortnite

\$

Best laptop for Excel

\$

Best laptop for Solidworks

\$

Best laptop for Roblox

\$

Best computer for crypto mining

\$

Best laptop for Sims 4

\$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

NUMPYNUMPY

How to specify multiple return types using type-hints

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

NUMPYNUMPY

glob exclude pattern

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

NUMPYNUMPY

Python CSV error: line contains NULL byte

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

## Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries