擅长:python、mysql、java
<p>您可以使用在<a href="https://stackoverflow.com/questions/5764782/iterate-through-pairs-of-items-in-a-python-list">Iterate through pairs of items in a Python list</a>中定义的<code>pairwise</code>函数,并将dict的<code>items()</code>传递给它:</p>
<pre><code>from itertools import tee
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = tee(iterable)
next(b, None)
return zip(a, b)
d = {0:0, 1:1, 2:2, 3:3}
list(map(dict,pairwise(d.items())))
# [{0: 0, 1: 1}, {1: 1, 2: 2}, {2: 2, 3: 3}]
</code></pre>
<p>如果需要非重复对,则可以使用<code>zip</code>:</p>
^{pr2}$
<p>字典基本上是无序的,所以如果您想要一个特定的顺序,您需要使用其他一些逻辑来指定它。在</p>