在 python 中加入字符串列表,并将每个字符串用引号括起来

我有:

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

我想要:

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

用 Python 做这件事最简单的方法是什么?

115133 次浏览

更新2021: 在 Python 3中使用 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"'
>>> ', '.join(['"%s"' % w for w in words])

您也可以执行单个 format调用

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

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

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

你可以试试这个:

str(words)[1:-1]

F String (用于 python 3.6 +)的@jamylak 答案的更新版本,我已经为 SQL 脚本使用的字符串使用了反勾。

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

找到更快的方法

'"' + '","'.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
# 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+"'")

产出:

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

有两个可能的解决办法,对我来说,我们应该明智地选择..。

items = ['A', 'B', 'C']


# Fast since we used only string concat.
print("\"" + ("\",\"".join(items)) + "\"")


# Slow since we used loop here.
print(",".join(["\"{item}\"".format(item=item) for item in items]))