ProgrammingHardPython · GATE DA · Advanced
What is printed?
What is printed?
import itertools data = [1, 1, 2, 1, 1, 1, 3] print([(k, len(list(g))) for k, g in itertools.groupby(data)]) print(sorted(itertools.groupby(data), key=lambda t: t[0])[0][0])
- A. [(1, 5), (2, 1), (3, 1)] / 3
- B. [(1, 2), (2, 1), (1, 3), (3, 1)] / 3
- C. [(1, 5), (2, 1), (3, 1)] / 1
- D. [(1, 2), (2, 1), (1, 3), (3, 1)] / 1
Correct answer: D. [(1, 2), (2, 1), (1, 3), (3, 1)] / 1
groupby only groups consecutive equal keys, so 1 appears as two separate runs (lengths 2 and 3); the second line only reads keys, so the smallest key is 1.
More Python Programming questions
- What is printed? python class P: def init(self, v): self.v = v def eq(self, o): return self.v == o.v s = set() try: s.add(P(1)) except…
- What is printed? python def make(): n = 0 def inc(): n += 1 return n return inc try: print(make()()) except Exception as e…
- What is printed? python import copy a = [1, 2] b = [a, a] c = copy.deepcopy(b) d = copy.copy(b) c[0].append(3) print(b, c[0] is c[1], d[0]…
- What is printed? python class M: def enter(self): print('in', end=' '); return self def exit(self, t, v, tb): print('exit', t.name if t…
- What is printed? python a = [0] 3 m = [a] 2 m[0][1] = 5 n = [[0] 3 for in range(2)] n[0][1] = 5 print(m, n, a)
- What is printed? python try: try: 1 / 0 except ZeroDivisionError as e: raise ValueError('bad') from None except ValueError as v…