擅长:python、mysql、java
<p>您不会一次读取两个字符,即使是这样,<code>map</code>也不会将它们拆分为<code>lambda</code>的两个参数。</p>
<pre><code>from itertools import tee, zip_longest
def pairwise(iterable, fillvalue):
""" Slightly modified itertools pairwise recipe
s -> (s0,s1), (s1,s2), (s2, s3), ...
"""
a, b = tee(iterable)
next(b, None)
return zip_longest(a, b, fillvalue=fillvalue)
def encode_arabic(verse):
was_diacritic = False # variable to keep track of diacritics stops us checking same character twice
# fillvalue will not be encoded but ensures last char is read
for this_char, next_char in pairwise(verse, fillvalue='-'):
if was_diacritic: # last next_char (so this_char) is diacritic
was_diacritic = False
elif is_diacritic(next_char):
yield encode(this_char + next_char)
was_diacritic = True
else:
yield this_char
encode_arabic(verse) # returns a generator like map wrap in list / string.join / whatever
</code></pre>