ProgrammingHardPython · GATE DA
What is printed?
What is printed?
from collections import Counter
c = Counter('mississippi')
print(c.most_common(2), c['z'], sorted(c.items())[:2])- A. [('i', 4), ('s', 4)] 0 [('i', 4), ('m', 1)]
- B. [('s', 4), ('i', 4)] 0 [('i', 4), ('m', 1)]
- C. [('i', 4), ('s', 4)] KeyError [('i', 4), ('m', 1)]
- D. [('i', 4), ('s', 4)] 0 [('i', 4), ('s', 4)]
Correct answer: A. [('i', 4), ('s', 4)] 0 [('i', 4), ('m', 1)]
most_common keeps first-seen order among ties (i before s); a missing key in a Counter returns 0 without raising; sorted(items) orders alphabetically (i, m, ...).
More Python Programming questions
- What is printed? python pts = [(2, 1), (1, 5), (2, 0), (1, 2)] print(sorted(pts, key=lambda p: (-p[0], p[1])), max(pts, key=lambda p: p[1]))
- What is printed? python import copy a = {'k': [1, 2]} b = a.copy() c = copy.deepcopy(a) a['k'].append(3) a['j'] = 0 print(b, c)
- What is printed? python m = map(lambda x: x 2, [1, 2, 3]) print(list(m), list(m), sum(map(int, '123')))
- What is printed? python class P: def init(self, v): self.v = v def eq(self, o): return self.v == o.v try: s = {P(1), P(1)} print(len(s))…
- What is printed? python fs = [lambda x, i=i: x i for i in range(3)] gs = [lambda x: x i for i in range(3)] print([f(2) for f in fs], [g(2)…
- What is printed? python n = 1 xs = [1, 2, 3] g = (x for x in xs if x > n) xs = [10, 20] n = 2 print(list(g))