ProgrammingVery hardCAT · MBA placement
What is printed?
What is printed?
d = {'b': 2, 'a': 2, 'c': 1}
print(sorted(d.items(), key=lambda kv: (-kv[1], kv[0])), sorted(d, key=d.get))- A. [('a', 2), ('b', 2), ('c', 1)] ['c', 'a', 'b']
- B. [('b', 2), ('a', 2), ('c', 1)] ['c', 'b', 'a']
- C. [('c', 1), ('a', 2), ('b', 2)] ['c', 'b', 'a']
- D. [('a', 2), ('b', 2), ('c', 1)] ['c', 'b', 'a']
Correct answer: D. [('a', 2), ('b', 2), ('c', 1)] ['c', 'b', 'a']
The first sort orders by negative value and then by key. The second sorts the keys by value, and ties keep dictionary insertion order, so b comes before a.
More Python Programming questions
- Which statement about itertools.groupby is correct?
- What is printed? python print(-1 >> 1, -8 >> 2, 1 << 70 > 1020, (-5) & 0xF)
- What is printed? python print(0 or [] or 'x', 2 and 0 and 5, None or 0)
- What is printed? python d = {} d.setdefault('k', []).append(1) d.setdefault('k', []).append(2) print(d, d.setdefault('j', 9), 'j' in d)
- What is printed? python w = ['pear', 'fig', 'apple', 'kiwi', 'date'] print(sorted(w, key=lambda s: (len(s), s)), sorted(w, key=len…
- What is printed? python print(bool(0.0), bool(' '), bool(None is None), bool(float('nan')))