连接字符串的最Pythonic方法 [英] Most Pythonic way to concatenate strings
问题描述
给出这个无害的小清单:
Given this harmless little list:
>>> lst = ['o','s','s','a','m','a']
我的目标是使用以下其中一种方式以Python的方式连接小恶魔:
My goal is to pythonically concatenate the little devils using one of the following ways:
A。 plain ol'string function可以完成工作,简短而又无需导入
A. plain ol' string function to get the job done, short, no imports
>>> ''.join(lst)
'ossama'
B。 lambda,lambda,lambda
B. lambda, lambda, lambda
>>> reduce(lambda x, y: x + y, lst)
'ossama'
C 。全球化(什么也不做,什么都导入)
C. globalization (do nothing, import everything)
>>> import functools, operator
>>> functools.reduce(operator.add, lst)
'ossama'
请建议其他pythonic
Please suggest other pythonic ways to achieve this magnanimous task.
请对(python级)排名和评分解决方案进行简要说明。
Please rank (pythonic level) and rate solutions giving concise explanations.
在这种情况下,最Python的解决方案是最好的编码解决方案吗?
In this case, is the most pythonic solution the best coding solution?
推荐答案
看看Guido的文章关于python优化,它涵盖了将数字列表转换为字符串的过程。除非您有好理由,否则请使用 join
示例。
Have a look at Guido's essay on python optimization, it covers converting lists of numbers to strings. Unless you have a good reason to do otherwise, use the join
example.
这篇关于连接字符串的最Pythonic方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!