python如何实现列表交叉合并?

WBOY
发布: 2016-06-06 16:24:35
原创
3808 人浏览过

a=[1,2,3,4,5,6]
b=['a','b','c','d','e']
实现合并后[1,'a',2,'b',3,'c'......]这种类型

回复内容:

<code class="language-python"><span class="kn">from</span> <span class="nn">compiler.ast</span> <span class="kn">import</span> <span class="n">flatten</span>

<span class="n">a</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">]</span>
<span class="n">b</span> <span class="o">=</span> <span class="p">[</span><span class="s">'a'</span><span class="p">,</span> <span class="s">'b'</span><span class="p">,</span> <span class="s">'c'</span><span class="p">,</span> <span class="s">'d'</span><span class="p">,</span> <span class="s">'e'</span><span class="p">]</span>

<span class="n">c</span> <span class="o">=</span> <span class="n">flatten</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">))</span>

<span class="k">print</span> <span class="n">c</span>

<span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="s">'a'</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="s">'b'</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="s">'c'</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="s">'d'</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="s">'e'</span><span class="p">]</span>
</code>
登录后复制
<code class="language-python"><span class="kn">import</span> <span class="nn">itertools</span>
<span class="nb">list</span><span class="p">(</span><span class="n">itertools</span><span class="o">.</span><span class="n">chain</span><span class="o">.</span><span class="n">from_iterable</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">)))</span>
</code>
登录后复制
办法有多种,其中一种,解题思路是模拟队列,由黄哥所写。 python如何实现列表交叉合并?
<code class="language-python"><span class="n">a</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">]</span>
<span class="n">b</span> <span class="o">=</span> <span class="p">[</span><span class="s">'a'</span><span class="p">,</span> <span class="s">'b'</span><span class="p">,</span> <span class="s">'c'</span><span class="p">,</span> <span class="s">'d'</span><span class="p">,</span> <span class="s">'e'</span><span class="p">]</span>
<span class="n">c</span> <span class="o">=</span> <span class="p">[]</span>
<span class="nb">map</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">:</span> <span class="n">c</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">])</span> <span class="k">if</span> <span class="bp">None</span> <span class="ow">not</span> <span class="ow">in</span> <span class="p">[</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">]</span> <span class="k">else</span> <span class="bp">None</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span>

<span class="k">print</span> <span class="n">c</span>
<span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="s">'a'</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="s">'b'</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="s">'c'</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="s">'d'</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="s">'e'</span><span class="p">]</span>
</code>
登录后复制
有规律的交叉合并可以通过for循环来实现的吧
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!