ProgrammingModerateTCS NQT · Infosys
What is printed?
What is printed?
words = 'the quick brown the lazy the'.split()
counts = {}
for w in words:
counts[w] = counts.get(w, 0) + 1
print(sorted(counts.items(), key=lambda kv: (-kv[1], kv[0]))[:3])- A. [('the', 3), ('quick', 1), ('brown', 1)]
- B. [('the', 3), ('brown', 1), ('lazy', 1)]
- C. [('the', 3), ('lazy', 1), ('brown', 1)]
- D. [('the', 3), ('brown', 1), ('quick', 1)]
Correct answer: B. [('the', 3), ('brown', 1), ('lazy', 1)]
The key sorts by descending count and then alphabetically, so among the words with count 1 the alphabetical order gives brown and then lazy. The key (-count, word) is the standard way to sort by two criteria with different directions.
More Python Programming questions
- What is printed? python print('%.3f' % 1.0005, '%.1f' % 0.25, '%.1f' % 0.35, round(0.35, 1))
- What is printed? python from collections import Counter c = Counter('mississippi') print(c.mostcommon(2), sorted(c.items(), key=lambda kv…
- What is printed? python x = 5 y = x if x > 3 else 0 + 1 if x else -1 print(y, (lambda a, b=2: a b)(3), (lambda a: sum(a))(1, 2, 3))
- What is printed? python print(sorted([2.0, 2, True, 1.5]))
- What is printed? python print(len({1: 'a', True: 'b', 1.0: 'c'}), list({True: 1, 1: 2}.keys()))
- What is printed? python print(int(1e308) > 10308, float('1e-400'), 5e-324 / 2, 5e-324 / 2 == 0)