我试图做一些类似于"共轭器"。
说我有一份结局清单:
endings = ['o', 'es', 'e', 'emos', 'eis', 'em']
我有一个动词根作为字符串:
root = "com"
我想这样做的方式是:
for ending in endings:
print root + ending
输出:
como
comes
come
comemos
comeis
comem
但我希望的结果是:
como, comes, come, comemos, comeis, comem
如何实现这一点(并且每个结果项都没有引号,最后一项之后没有逗号)?
答案 0 :(得分: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