ProgrammingModerateTCS NQT · Wipro pattern
What is printed?
What is printed?
from collections import defaultdict
d = defaultdict(int)
for w in 'a b a c'.split():
d[w] += 1
print(sorted(d.items(), key=lambda kv: (-kv[1], kv[0])))- A. [('a', 2), ('c', 1), ('b', 1)]
- B. [('a', 2), ('b', 1), ('c', 1)]
- C. [('b', 1), ('a', 2), ('c', 1)]
- D. [('a', 2), ('b', 1)]
Correct answer: B. [('a', 2), ('b', 1), ('c', 1)]
The counts are a=2, b=1 and c=1. The key sorts by descending count and then alphabetically, so the order is a, b, c.
More Python Programming questions
- What is printed? python print(sum([1e100, 1.0, -1e100]) == 1.0, sum([1, 1e100, -1e100]), 1e100 + 1.0 - 1e100)
- What is printed on the third line? python emp = [('Ravi', 30, 'IT'), ('Asha', 25, 'HR'), ('Meera', 30, 'HR'), ('Dev', 25, 'IT')] byage =…
- What is printed on the second line? python items = [3, -1, 2, -3, 1] print(sorted(items, key=abs)) print(sorted(items, key=abs…
- What is printed on the second line? python data = [('b', 2), ('a', 2), ('c', 1), ('a', 1)] print(sorted(data, key=lambda t: (t[1], t[0])…
- What is printed on the second line? python words = ['bb', 'a', 'ccc', 'dd', 'e'] print(sorted(words, key=len)) print(sorted(words…
- What is printed on the last line? python nums = ['10', '9', '100', '1'] print(sorted(nums)) print(sorted(nums, key=int)) print(max(nums)…