<small id='m6y7u'></small><noframes id='m6y7u'>

<legend id='m6y7u'><style id='m6y7u'><dir id='m6y7u'><q id='m6y7u'></q></dir></style></legend>
<i id='m6y7u'><tr id='m6y7u'><dt id='m6y7u'><q id='m6y7u'><span id='m6y7u'><b id='m6y7u'><form id='m6y7u'><ins id='m6y7u'></ins><ul id='m6y7u'></ul><sub id='m6y7u'></sub></form><legend id='m6y7u'></legend><bdo id='m6y7u'><pre id='m6y7u'><center id='m6y7u'></center></pre></bdo></b><th id='m6y7u'></th></span></q></dt></tr></i><div id='m6y7u'><tfoot id='m6y7u'></tfoot><dl id='m6y7u'><fieldset id='m6y7u'></fieldset></dl></div>
    <bdo id='m6y7u'></bdo><ul id='m6y7u'></ul>
  • <tfoot id='m6y7u'></tfoot>

      1. Python:将列表与范围列表合并

        时间:2023-11-08

        1. <i id='7dMUD'><tr id='7dMUD'><dt id='7dMUD'><q id='7dMUD'><span id='7dMUD'><b id='7dMUD'><form id='7dMUD'><ins id='7dMUD'></ins><ul id='7dMUD'></ul><sub id='7dMUD'></sub></form><legend id='7dMUD'></legend><bdo id='7dMUD'><pre id='7dMUD'><center id='7dMUD'></center></pre></bdo></b><th id='7dMUD'></th></span></q></dt></tr></i><div id='7dMUD'><tfoot id='7dMUD'></tfoot><dl id='7dMUD'><fieldset id='7dMUD'></fieldset></dl></div>

        2. <tfoot id='7dMUD'></tfoot>
              <tbody id='7dMUD'></tbody>
          • <legend id='7dMUD'><style id='7dMUD'><dir id='7dMUD'><q id='7dMUD'></q></dir></style></legend>

            <small id='7dMUD'></small><noframes id='7dMUD'>

              <bdo id='7dMUD'></bdo><ul id='7dMUD'></ul>

                • 本文介绍了Python:将列表与范围列表合并的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个列表:

                  L = ['a', 'b']
                  

                  我需要通过连接从 1k 的原始 list 来创建一个新的 list.示例:

                  I need to create a new list by concatenating an original list which range goes from 1 to k. Example:

                  k = 4
                  L1 = ['a1','b1', 'a2','b2','a3','b3','a4','b4']
                  

                  我试试:

                  l1 = L * k
                  print l1
                  #['a', 'b', 'a', 'b', 'a', 'b', 'a', 'b']
                  
                  l = [ [x] * 2  for x in range(1, k + 1) ]
                  print l
                  #[[1, 1], [2, 2], [3, 3], [4, 4]]
                  
                  l2 = [item for sublist in l for item in sublist]
                  print l2
                  #[1, 1, 2, 2, 3, 3, 4, 4]
                  
                  print zip(l1,l2)
                  #[('a', 1), ('b', 1), ('a', 2), ('b', 2), ('a', 3), ('b', 3), ('a', 4), ('b', 4)]
                  
                  print [x+ str(y) for x,y in zip(l1,l2)] 
                  #['a1', 'b1', 'a2', 'b2', 'a3', 'b3', 'a4', 'b4']
                  

                  但我认为这很复杂.什么是最快和最通用的解决方案?

                  But I think it is very complicated. What is the fastest and most generic solution?

                  推荐答案

                  你可以使用列表推导:

                  L = ['a', 'b']
                  k = 4
                  L1 = ['{}{}'.format(x, y) for y in range(1, k+1) for x in L]
                  print(L1)
                  

                  输出

                  ['a1', 'b1', 'a2', 'b2', 'a3', 'b3', 'a4', 'b4']
                  

                  这篇关于Python:将列表与范围列表合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:创建具有固定数量元素(长度)的范围 下一篇:Python: Range() 最大尺寸;动态的还是静态的?

                  相关文章

                  • <bdo id='YcUoi'></bdo><ul id='YcUoi'></ul>

                • <tfoot id='YcUoi'></tfoot>
                  <i id='YcUoi'><tr id='YcUoi'><dt id='YcUoi'><q id='YcUoi'><span id='YcUoi'><b id='YcUoi'><form id='YcUoi'><ins id='YcUoi'></ins><ul id='YcUoi'></ul><sub id='YcUoi'></sub></form><legend id='YcUoi'></legend><bdo id='YcUoi'><pre id='YcUoi'><center id='YcUoi'></center></pre></bdo></b><th id='YcUoi'></th></span></q></dt></tr></i><div id='YcUoi'><tfoot id='YcUoi'></tfoot><dl id='YcUoi'><fieldset id='YcUoi'></fieldset></dl></div>

                  <small id='YcUoi'></small><noframes id='YcUoi'>

                • <legend id='YcUoi'><style id='YcUoi'><dir id='YcUoi'><q id='YcUoi'></q></dir></style></legend>