【问题标题】:Join a list of strings in python and wrap each string in quotation marks加入python中的字符串列表并将每个字符串用引号引起来
【发布时间】:2012-08-14 00:20:06
【问题描述】:

我有:

words = ['hello', 'world', 'you', 'look', 'nice']

我想拥有:

'"hello", "world", "you", "look", "nice"'

用 Python 最简单的方法是什么?

【问题讨论】:

    标签: python string list


    【解决方案1】:

    2021 年更新:在 Python3 中使用 f 个字符串

    >>> words = ['hello', 'world', 'you', 'look', 'nice']
    >>> ', '.join(f'"{w}"' for w in words)
    '"hello", "world", "you", "look", "nice"'
    

    原始答案(支持 Python 2.6+)

    >>> words = ['hello', 'world', 'you', 'look', 'nice']
    >>> ', '.join('"{0}"'.format(w) for w in words)
    '"hello", "world", "you", "look", "nice"'
    

    【讨论】:

    • @Meow 使用 repr 在这种特定情况下这是一个小技巧,而不是用引号明确
    • @jamlak 好的,repr 对我来说似乎更安全,因为您的字符串中有引号。
    【解决方案2】:

    找到更快的方法

    '"' + '","'.join(words) + '"'
    

    在 Python 2.7 中测试:

        words = ['hello', 'world', 'you', 'look', 'nice']
    
        print '"' + '","'.join(words) + '"'
        print str(words)[1:-1]
        print '"{0}"'.format('", "'.join(words))
    
        t = time() * 1000
        range10000 = range(100000)
    
        for i in range10000:
            '"' + '","'.join(words) + '"'
    
        print time() * 1000 - t
        t = time() * 1000
    
        for i in range10000:
            str(words)[1:-1]
        print time() * 1000 - t
    
        for i in range10000:
            '"{0}"'.format('", "'.join(words))
    
        print time() * 1000 - t
    

    结果输出是:

    # "hello", "world", "you", "look", "nice"
    # 'hello', 'world', 'you', 'look', 'nice'
    # "hello", "world", "you", "look", "nice"
    # 39.6000976562
    # 166.892822266
    # 220.110839844
    

    【讨论】:

      【解决方案3】:

      @jamylak 使用 F 字符串回答的更新版本(适用于 python 3.6+),我对用于 SQL 脚本的字符串使用了反引号。

      keys = ['foo', 'bar' , 'omg']
      ', '.join(f'`{k}`' for k in keys)
      # result: '`foo`, `bar`, `omg`'
      

      【讨论】:

      • 有效,但它被认为是规范的pythonic方式吗?
      • @MarcelFlygare 是的f 字符串似乎是现在最好的方式
      【解决方案4】:

      您也可以执行单个format 呼叫

      >>> words = ['hello', 'world', 'you', 'look', 'nice']
      >>> '"{0}"'.format('", "'.join(words))
      '"hello", "world", "you", "look", "nice"'
      

      更新:一些基准测试(在 2009 mbp 上执行):

      >>> timeit.Timer("""words = ['hello', 'world', 'you', 'look', 'nice'] * 100; ', '.join('"{0}"'.format(w) for w in words)""").timeit(1000)
      0.32559704780578613
      
      >>> timeit.Timer("""words = ['hello', 'world', 'you', 'look', 'nice'] * 100; '"{}"'.format('", "'.join(words))""").timeit(1000)
      0.018904924392700195
      

      看来format其实挺贵的

      更新 2:在@JCode 的评论之后,添加map 以确保join 可以正常工作,Python 2.7.12

      >>> timeit.Timer("""words = ['hello', 'world', 'you', 'look', 'nice'] * 100; ', '.join('"{0}"'.format(w) for w in words)""").timeit(1000)
      0.08646488189697266
      
      >>> timeit.Timer("""words = ['hello', 'world', 'you', 'look', 'nice'] * 100; '"{}"'.format('", "'.join(map(str, words)))""").timeit(1000)
      0.04855608940124512
      
      >>> timeit.Timer("""words = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] * 100; ', '.join('"{0}"'.format(w) for w in words)""").timeit(1000)
      0.17348504066467285
      
      >>> timeit.Timer("""words = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] * 100; '"{}"'.format('", "'.join(map(str, words)))""").timeit(1000)
      0.06372308731079102
      

      【讨论】:

      • 这个比jamylak提出的性能更好吗?
      • 这是一个比公认答案更好的解决方案。
      • @sage88 不,不是。预优化是邪恶的,这里微小的速度差异有 0.0000001% 的可能性是 Python 脚本的整个瓶颈。此外,这段代码不太直观,所以它不是更好,而是稍微快一点。我的解决方案更具pythonic和可读性
      • @marchelbling 基准测试无效,因为 jamylak 的解决方案也适用于非字符串的迭代。将 .join(words) 替换为 .join(map(str, words)) 并向我们展示这是怎么回事。
      • @JCode 更新了基准。差距更小,但我的机器上仍有 2 倍的增益。
      【解决方案5】:

      你可以试试这个:

      str(words)[1:-1]
      

      【讨论】:

      • 怎么加引号?
      • 这会添加单引号而不是双引号,但是单词中的引号将被自动转义。 +1 聪明。
      • 这是让 Python 如此有价值的聪明花絮之一。
      • 这太棒了,只是喜欢它,但我可能会使用其他解决方案,只是为了让我的开发人员更直观的代码。
      • 这就是为什么我喜欢 Python,如此优雅
      【解决方案6】:
      >>> ', '.join(['"%s"' % w for w in words])
      

      【讨论】:

      • 格式自python 2.6起可用。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-09
      • 1970-01-01
      • 2017-10-17
      • 2020-06-02
      相关资源
      最近更新 更多