I need your help to solve a problem.
I want to convert a dictionary d = {key1:value1, key2:value2}
into
list= [keys1, keys1, ... (value1 times), keys2, ... (value2 times)]
without using a nested loop.
Example:
d1 = {4: 1, 3: 2, 12: 2}
The code should produce the output:
l = [4, 3, 3, 12, 12]
This is what I have:
for key, value in nums1.items():
temp = (str(key))*value
nums2.append(int(temp))
print(nums2)
Which gives: [4, 33, 1212]
, but should give [4, 3, 3, 12, 12]
.
The complexity should be O(n).
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…