简体   繁体   中英

Join a list of strings in python and wrap each string in quotation marks

I've got:

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

I want to have:

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

What's the easiest way to do this with Python?

Update 2021: With f strings in Python3

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

Original Answer (Supports Python 2.6+)

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

you may also perform a single format call

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

Update: Some benchmarking (performed on a 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

So it seems that format is actually quite expensive

Update 2: following @JCode's comment, adding a map to ensure that join will work, 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

你可以试试这个:

str(words)[1:-1]
>>> ', '.join(['"%s"' % w for w in words])

An updated version of @jamylak answer with F Strings (for python 3.6+), I've used backticks for a string used for a SQL script.

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

find a faster way

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

test in 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

The resulting output is:

# "hello", "world", "you", "look", "nice"
# 'hello', 'world', 'you', 'look', 'nice'
# "hello", "world", "you", "look", "nice"
# 39.6000976562
# 166.892822266
# 220.110839844
# Python3 without for loop
conc_str = "'{}'".format("','".join(['a', 'b', 'c']))
print(conc_str) 

# "'a', 'b', 'c'"



    

words = ['hello', 'world', 'you', 'look', 'nice']
S = ""
for _ in range(len(words)-1):
    S+=f'"{words[_]}"'+', '
S +=f'"{words[len(words)-1]}"'
print("'"+S+"'")

OUTPUT:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM