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

WBOY
Libérer: 2016-06-06 16:24:35
original
3808 Les gens l'ont consulté

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>
Copier après la connexion
<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>
Copier après la connexion
办法有多种,其中一种,解题思路是模拟队列,由黄哥所写。 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>
Copier après la connexion
有规律的交叉合并可以通过for循环来实现的吧
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!