<p>今天早些时候,我需要一次遍历一个字符串2个字符来解析格式类似<code>"+c-R+D-E"</code>的字符串(还有一些额外的字母)。</p>
<p>我最终得到了这个,这很管用,但看起来很难看。我最后评论了它在做什么,因为它感觉不明显。看起来像是Python,但不完全是。</p>
<pre><code># Might not be exact, but you get the idea, use the step
# parameter of range() and slicing to grab 2 chars at a time
s = "+c-R+D-e"
for op, code in (s[i:i+2] for i in range(0, len(s), 2)):
print op, code
</code></pre>
<p>有没有更好/更干净的方法来做这个?</p>