<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:12.0pt;
font-family:"Times New Roman",serif;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
span.EmailStyle17
{mso-style-type:personal-reply;
font-family:"Calibri",sans-serif;
color:#1F497D;}
.MsoChpDefault
{mso-style-type:export-only;
font-family:"Calibri",sans-serif;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.25in 1.0in 1.25in;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D">It would be faster with ‘deque’:<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D">def roundrobin(*iterables):<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D"> iters = deque(map(iter,iterables), len(iterables))<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D"> while iters:<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D"> try:<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D"> yield next(iters[0])<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D"> except StopIteration:<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D"> iters.popleft()<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D"> else:<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D"> iters.rotate(-1)<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><b><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">From:</span></b><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"> Wes Turner [mailto:wes.turner@gmail.com]
<br>
<b>Sent:</b> Wednesday, November 22, 2017 04:11<br>
<b>To:</b> Alon Snir <AlonSnir@hotmail.com><br>
<b>Cc:</b> python-ideas@python.org<br>
<b>Subject:</b> Re: [Python-ideas] Rewriting the "roundrobin" recipe in the itertools documentation<o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Here's toolz.itertoolz.interleave():<o:p></o:p></p>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<div>
<p class="MsoNormal">def interleave(seqs):<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> """ Interleave a sequence of sequences<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> >>> list(interleave([[1, 2], [3, 4]]))<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> [1, 3, 2, 4]<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> >>> ''.join(interleave(('ABC', 'XY')))<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> 'AXBYC'<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> Both the individual sequences and the sequence of sequences may be infinite<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> Returns a lazy iterator<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> """<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> iters = itertools.cycle(map(iter, seqs))<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> while True:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> try:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> for itr in iters:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> yield next(itr)<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> return<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> except StopIteration:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> predicate = partial(operator.is_not, itr)<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> iters = itertools.cycle(itertools.takewhile(predicate, iters))<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">At first glance, map should be e.g. six.moves.map for pythonic backward compatibility.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Is this slower than the linear time implementations listed here?<o:p></o:p></p>
</div>
<p class="MsoNormal"><br>
On Tuesday, November 21, 2017, Alon Snir <<a href="mailto:AlonSnir@hotmail.com">AlonSnir@hotmail.com</a>> wrote:<o:p></o:p></p>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in">
<p class="MsoNormal">def roundrobin(*iterables):<br>
"roundrobin('ABC', 'D', 'EF') --> A D E B F C"<br>
nexts = [ iter(it).__next__ for it in iterables ]<br>
i = 0<br>
while nexts:<br>
i %= len(nexts)<br>
try:<br>
yield nexts[i]()<br>
except StopIteration:<br>
del nexts[i]<br>
else:<br>
i += 1<br>
<br>
Regards<br>
Alon Snir<br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="javascript:;">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" target="_blank">http://python.org/psf/codeofconduct/</a><o:p></o:p></p>
</blockquote>
</div>
</div>
</body>
</html>