gpt4 book ai didi

python - 从列表到元组

转载 作者:太空宇宙 更新时间:2023-11-03 14:43:07 24 4
gpt4 key购买 nike

我正在努力改进这个code使其在 Python 3.6 中更加高效。

row 是一个列表(来自 csv 阅读器)

sql = "CREATE TABLE %s (%s)" % (tablename,", ".join([ "%s text" % column for column in row ]))

我不确定这是否是最好的方法,但到目前为止,我能够通过替换前两个 %s 但不能替换第三个来改进代码。

query = 'CREATE TABLE  {0} ({1})'
sql = query.format(tablename, ", ".join([ "%s text" % column for column in row ]))

是否可以替换第三个%s?使用格式是最好的方法吗?

最佳答案

当然,这是可能的。

sql = "CREATE TABLE {0} ({1})".format(tablename,
','.join(["{} text".format(column) for column in row])

这主要是个人喜好问题。一方面,我总是更喜欢使用 format 而不是 c 风格的格式化语法。

关于python - 从列表到元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46450768/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com