python3练习100题——033

链接:http://www.runoob.com/python/python-exercise-example33.html

题目:按逗号分隔列表。

l=[1,2,3,4]
k=1
for i in l:
    if k !=len(l):
        print(i,end=',')
    else:
        print(i)
    k+=1

思考:

可以用join函数!

str.join(sequence)

sequence可以是list\tuple等。

str是链接符。

返回值是一个链接好了的字符串。

l=[1,2,3,4]
s=','.join(str(i) for i in l)    #sequence里的元素必须是str,不然要用这种形式转换。
print(s)

猜你喜欢

转载自www.cnblogs.com/drifter/p/9197348.html