如何避免python循环中的最后一个逗号 [英] How to avoid last comma in python loop

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

问题描述

我想以逗号分隔的形式打印结果输出,但我在最后一个值中也得到一个逗号,所以我该如何删除它.

I want to print the result output in the form of separated by a comma but i am getting a comma in last value also so how can i remove that.

import math

    d = input().split(',')
    d = [int(i) for i in d]
    c=50
    h=30
    result=[]
    for i in d:
        q=int(round(math.sqrt((2*c*i)/h)))
        result.append(q)
    for i in result:
        print(i, end=",")

这是我给出的输入和我得到的输出的示例

here is an example of input i give and output i get

input : 10,20,30,40
output : 6,8,10,12,

我怎样才能避免得到最后一个逗号

how can i avoid getting that last comma

推荐答案

将结果作为单独的参数传递并指定分隔符:

Pass the results as separate arguments and specify the separator:

 print(*result, sep=',')

这篇关于如何避免python循环中的最后一个逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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