擅长:python、mysql、java
<p>您需要对代码进行两项更改:</p>
<ol>
<li><p>既然可以用相同的结果迭代<code>zip(the_dict.keys(), the_dict.values())</code>,为什么还要迭代<code>the_dict.items()</code></p>
</li>
<li><p>请注意,如果将字符串括在列表中,则不管字符串有多长:</p>
</li>
</ol>
<pre class="lang-py prettyprint-override"><code>print(["10"]*2)
# Prints ['10', '10']
print(["1"]*2)
# Prints ['1', '1']
</code></pre>
<p>另外,正如wim指出的,<code>list</code>函数调用是不必要的,因为您已经在创建一个列表</p>
<p>尽管如此,下面是更正的代码:</p>
<pre class="lang-py prettyprint-override"><code>the_dict = {'1': 2,
'2': 4,
'3': 3,
'8': 3,
'9': 3,
'10': 4,
'14': 4}
new_target = []
for k, v in the_dict.items():
new_target += [k]*v
print(new_target)
# Prints ['14', '14', '14', '14', '10', '10', '10', '10', '2', '2', '2', '2', '1', '1',
# '3', '3', '3', '9', '9', '9', '8', '8', '8']
</code></pre>
<p>查看diggy的答案,获得一个简短的列表理解版本</p>