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

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

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

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

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

        时间:2023-06-06
        <i id='GLqIV'><tr id='GLqIV'><dt id='GLqIV'><q id='GLqIV'><span id='GLqIV'><b id='GLqIV'><form id='GLqIV'><ins id='GLqIV'></ins><ul id='GLqIV'></ul><sub id='GLqIV'></sub></form><legend id='GLqIV'></legend><bdo id='GLqIV'><pre id='GLqIV'><center id='GLqIV'></center></pre></bdo></b><th id='GLqIV'></th></span></q></dt></tr></i><div id='GLqIV'><tfoot id='GLqIV'></tfoot><dl id='GLqIV'><fieldset id='GLqIV'></fieldset></dl></div>
      3. <tfoot id='GLqIV'></tfoot>
        • <bdo id='GLqIV'></bdo><ul id='GLqIV'></ul>

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

                <tbody id='GLqIV'></tbody>

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

                  问题描述

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

                  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
                  

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

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

                  相关文章

                    <bdo id='DJ3FR'></bdo><ul id='DJ3FR'></ul>

                1. <small id='DJ3FR'></small><noframes id='DJ3FR'>

                2. <tfoot id='DJ3FR'></tfoot>

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