Python将字符串与列表的所有成员连接起来,并以逗号分隔显示每个显示结果

Ped*_*ins 0 python list concatenation

我正试图做一些像"共轭者"的事情.

说我有一个结尾列表:

endings = ['o', 'es', 'e', 'emos', 'eis', 'em']
Run Code Online (Sandbox Code Playgroud)

我有一个动词根作为字符串:

root = "com"
Run Code Online (Sandbox Code Playgroud)

我想这样做的方式是:

for ending in endings:
    print root + ending
Run Code Online (Sandbox Code Playgroud)

哪个输出:

como
comes
come
comemos
comeis
comem
Run Code Online (Sandbox Code Playgroud)

但我想要的结果是:

como, comes, come, comemos, comeis, comem
Run Code Online (Sandbox Code Playgroud)

我怎样才能达到这个目的(并且每个结果项都没有引号,最后一项之后没有逗号)?

Seb*_*zny 6

你需要一个列表理解和str.join().从文档:

str.join(iterable)

返回一个字符串,该字符串是可迭代迭代中字符串的串联.元素之间的分隔符是提供此方法的字符串.

>>> root = "com"
>>> endings = ['o', 'es', 'e', 'emos', 'eis', 'em']
>>> verbs = [root + ending for ending in endings]
>>> print ", ".join(verbs)
como, comes, come, comemos, comeis, comem
Run Code Online (Sandbox Code Playgroud)