![](/img/trans.png)
[英]python join() list to string then giving me quotation marks in my mongoDB query
[英]Join a list of strings in python and wrap each string in quotation marks
我有:
words = ['hello', 'world', 'you', 'look', 'nice']
我希望有:
'"hello", "world", "you", "look", "nice"'
使用 Python 最簡單的方法是什么?
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"'
您也可以執行單一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
你可以試試這個:
str(words)[1:-1]
>>> ', '.join(['"%s"' % w for w in words])
@jamylak 的更新版本使用 F 字符串回答(對於 python 3.6+),我對用於 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+"'")
OUTPUT:
'"hello", "world", "you", "look", "nice"'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.