Collection Module


>>> import collections

>>> from collections import namedtuple

>>> a = namedtuple('course','name','technology')

Traceback (most recent call last):

  File "<pyshell#2>", line 1, in <module>

    a = namedtuple('course','name','technology')

TypeError: namedtuple() takes 2 positional arguments but 3 were given

>>> a = namedtuple('course','name,technology')

>>> s=('data science','python')

>>> s

('data science', 'python')

>>> s=a.make(['artificial intelligence','python'])

Traceback (most recent call last):

  File "<pyshell#6>", line 1, in <module>

    s=a.make(['artificial intelligence','python'])

AttributeError: type object 'course' has no attribute 'make'

>>> from collections import deque

>>> a=['e','d','u','r','e','k','a']

>>> d=deque(a)

>>> d

deque(['e', 'd', 'u', 'r', 'e', 'k', 'a'])

>>> d.append('python')

>>> d

deque(['e', 'd', 'u', 'r', 'e', 'k', 'a', 'python'])

>>> d.apendleft('python')

Traceback (most recent call last):

  File "<pyshell#13>", line 1, in <module>

    d.apendleft('python')

AttributeError: 'collections.deque' object has no attribute 'apendleft'

>>> d.appendleft('python')

>>> d

deque(['python', 'e', 'd', 'u', 'r', 'e', 'k', 'a', 'python'])

>>> d.pop()

'python'

>>> d

deque(['python', 'e', 'd', 'u', 'r', 'e', 'k', 'a'])

>>> d.popleft()

'python'

>>> d

deque(['e', 'd', 'u', 'r', 'e', 'k', 'a'])

>>> from collections import chainmap

Traceback (most recent call last):

  File "<pyshell#20>", line 1, in <module>

    from collections import chainmap

ImportError: cannot import name 'chainmap' from 'collections' (C:\Python\Python39\lib\collections\__init__.py)

>>> from collections import Chainmap

Traceback (most recent call last):

  File "<pyshell#21>", line 1, in <module>

    from collections import Chainmap

ImportError: cannot import name 'Chainmap' from 'collections' (C:\Python\Python39\lib\collections\__init__.py)

>>> from collections import ChainMap

>>> a={1:'edureka',2:'python'}

>>> b={3:'ML',4:'AI'}

>>> a1=ChainMap(a,b)

>>> a1

ChainMap({1: 'edureka', 2: 'python'}, {3: 'ML', 4: 'AI'})

>>> from collections import Counter

>>> #count hashable obj

>>> a=[1,1,2,2,3,3,1,3,2,3,4,2,4,2,1]

>>> c=Counter(a)

>>> c

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

>>> list(c.elements())

[1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4]

>>> lis=list(c.elements())

>>> lis.sort()

>>> lis

[1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4]

>>> c.most_commom()

Traceback (most recent call last):

  File "<pyshell#36>", line 1, in <module>

    c.most_commom()

AttributeError: 'Counter' object has no attribute 'most_commom'

>>> c.most_common()

[(2, 5), (1, 4), (3, 4), (4, 2)]

>>> sub={2:1 , 3:2}

>>> c.subtract(sub)

>>> print(c.subtract(sub))

None

>>> c.most_common()

[(1, 4), (2, 3), (4, 2), (3, 0)]

>>> c

Counter({1: 4, 2: 3, 4: 2, 3: 0})

>>> list(c.elements())

[1, 1, 1, 1, 2, 2, 2, 4, 4]

>>> from collections import OrderedDict

>>> d=OrderedDict()

>>> d[1]='e'

>>> d[2]='d'

>>> d[3]='u'

>>> print(d)

OrderedDict([(1, 'e'), (2, 'd'), (3, 'u')])

>>> d.keys()

odict_keys([1, 2, 3])

>>> d.items()

odict_items([(1, 'e'), (2, 'd'), (3, 'u')])

>>> d[1]='p'

>>> d

OrderedDict([(1, 'p'), (2, 'd'), (3, 'u')])

>>> from collections import defaultdict

>>> #avoid key value error

>>> 

Comments

Popular posts from this blog

Sum of Even Numbers till N

Find the Runner-Up Score!

Print All Substrings