Python:如何在for循环的最后一个元素中不显示逗号? [英] Python: How to not print comma in last element in a for loop?

查看:91
本文介绍了Python:如何在for循环的最后一个元素中不显示逗号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码遍历集合并打印演员的名字:

My code iterates over a set and print actor's names:

for actor in actorsByMovies():
    print actor+",",

结果如下:

Brad Pitt, George Clooney,

但是我希望它检测到最后一个元素,以便它不会显示最后一个逗号.结果应改为:

But I want it to detect the last element so that it won't print the last comma. The result should be instead:

Brad Pitt, George Clooney

我该怎么做?

推荐答案

print ', '.join(actorsByMovies())

这篇关于Python:如何在for循环的最后一个元素中不显示逗号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆