擅长:python、mysql、java
<pre><code>#Given a dictionary
>>> test ={'line4': (4, 2), 'line3': (3, 2), 'line2': (2, 2), 'line1': (1, 2), 'line10': (10, 2)}
#And if you want a list of tuples, what you need actually is the values of the dictionary
>>> test.values()
[(4, 2), (3, 2), (2, 2), (1, 2), (10, 2)]
#Instead if you want a flat list of values, you can flatten using chain/chain.from_iterable
>>> list(chain(*test.values()))
[4, 2, 3, 2, 2, 2, 1, 2, 10, 2]
#And to print the list
>>> for v in chain.from_iterable(test.values()):
print v
4
2
3
2
2
2
1
2
10
2
</code></pre>
<p>正在分析代码</p>
^{pr2}$
<ol>
<li>你不能索引字典。字典不像列表那样是一个序列</li>
<li>不要用括号来索引。它变成了一个函数调用</li>
<li>要迭代字典,可以迭代键或值。
<ol>
<li><code>for key in test</code>按键迭代字典</li>
<li><code>for key in test.values()</code>按值迭代字典</li>
</ol></li>
</ol>