• <small id='rBQaB'></small><noframes id='rBQaB'>

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

    • <bdo id='rBQaB'></bdo><ul id='rBQaB'></ul>
  • <legend id='rBQaB'><style id='rBQaB'><dir id='rBQaB'><q id='rBQaB'></q></dir></style></legend>

      <tfoot id='rBQaB'></tfoot>

        将两个列表加入格式化字符串的最聪明方法

        时间:2023-10-08
        <i id='eAANs'><tr id='eAANs'><dt id='eAANs'><q id='eAANs'><span id='eAANs'><b id='eAANs'><form id='eAANs'><ins id='eAANs'></ins><ul id='eAANs'></ul><sub id='eAANs'></sub></form><legend id='eAANs'></legend><bdo id='eAANs'><pre id='eAANs'><center id='eAANs'></center></pre></bdo></b><th id='eAANs'></th></span></q></dt></tr></i><div id='eAANs'><tfoot id='eAANs'></tfoot><dl id='eAANs'><fieldset id='eAANs'></fieldset></dl></div>

          <tbody id='eAANs'></tbody>
          • <bdo id='eAANs'></bdo><ul id='eAANs'></ul>

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

                <legend id='eAANs'><style id='eAANs'><dir id='eAANs'><q id='eAANs'></q></dir></style></legend>

              1. <tfoot id='eAANs'></tfoot>
                • 本文介绍了将两个列表加入格式化字符串的最聪明方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  假设我有两个相同长度的列表:

                  Lets say I have two lists of same length:

                  a = ['a1', 'a2', 'a3']
                  b = ['b1', 'b2', 'b3']
                  

                  我想生成以下字符串:

                  c = 'a1=b1, a2=b2, a3=b3'
                  

                  实现这一目标的最佳方法是什么?

                  What is the best way to achieve this?

                  我有以下实现:

                  import timeit
                  
                  a = [str(f) for f in range(500)]
                  b = [str(f) for f in range(500)]
                  
                  def func1():
                      return ', '.join([aa+'='+bb for aa in a for bb in b if a.index(aa) == b.index(bb)])
                  
                  def func2():
                      list = []
                      for i in range(len(a)):
                          list.append('%s=%s' % (a[i], b[i]))
                      return ', '.join(list)
                  
                  t = timeit.Timer(setup='from __main__ import func1', stmt='func1()')
                  print 'func1 = ' + t.timeit(10) 
                  
                  t = timeit.Timer(setup='from __main__ import func2', stmt='func2()')
                  print 'func2 = ' + t.timeit(10)
                  

                  输出是:

                  func1 = 32.4704790115
                  func2 = 0.00529003143311
                  

                  你有一些权衡吗?

                  推荐答案

                  a = ['a1', 'a2', 'a3']
                  b = ['b1', 'b2', 'b3']
                  
                  pat = '%s=%%s, %s=%%s, %s=%%s'
                  
                  print pat % tuple(a) % tuple(b)
                  

                  给出 a1=b1, a2=b2, a3=b3

                  .

                  然后:

                  from timeit import Timer
                  from itertools import izip
                  
                  n = 300
                  
                  a = [str(f) for f in range(n)]
                  b = [str(f) for f in range(n)]
                  
                  def func1():
                      return ', '.join([aa+'='+bb for aa in a for bb in b if a.index(aa) == b.index(bb)])
                  
                  def func2():
                      list = []
                      for i in range(len(a)):
                          list.append('%s=%s' % (a[i], b[i]))
                      return ', '.join(list)
                  
                  def func3():
                      return ', '.join('%s=%s' % t for t in zip(a, b))
                  
                  def func4():
                      return ', '.join('%s=%s' % t for t in izip(a, b))
                  
                  def func5():
                      pat = n * '%s=%%s, '
                      return pat % tuple(a) % tuple(b)
                  
                  d = dict(zip((1,2,3,4,5),('heavy','append','zip','izip','% formatting')))
                  for i in xrange(1,6):
                      t = Timer(setup='from __main__ import func%d'%i, stmt='func%d()'%i)
                      print 'func%d = %s  %s' % (i,t.timeit(10),d[i])
                  

                  结果

                  func1 = 16.2272833558  heavy
                  func2 = 0.00410247671143  append
                  func3 = 0.00349569568199  zip
                  func4 = 0.00301686387516  izip
                  func5 = 0.00157338432678  % formatting
                  

                  这篇关于将两个列表加入格式化字符串的最聪明方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在 Python 中用空格将字符串填充到固定长度 下一篇:在 Python 中将行号添加到输出中

                  相关文章

                  最新文章

                  <legend id='imknO'><style id='imknO'><dir id='imknO'><q id='imknO'></q></dir></style></legend>

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

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