相关疑难解决方法(0)

在Python中没有[]的列表理解

加入清单:

>>> ''.join([ str(_) for _ in xrange(10) ])
'0123456789'
Run Code Online (Sandbox Code Playgroud)

join 必须采取迭代.

显然,join这个论点是[ str(_) for _ in xrange(10) ],这是一个列表理解.

看这个:

>>>''.join( str(_) for _ in xrange(10) )
'0123456789'
Run Code Online (Sandbox Code Playgroud)

现在,join这个论点只是str(_) for _ in xrange(10),不[],但结果是一样的.

为什么?是否str(_) for _ in xrange(10)也会产生一个列表或一个可迭代?

python list-comprehension

76
推荐指数
4
解决办法
1万
查看次数

如何避免python循环中的最后一个逗号

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

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=",")
Run Code Online (Sandbox Code Playgroud)

这是一个输入的例子我给予和输出我得到

input : 10,20,30,40
output : 6,8,10,12,
Run Code Online (Sandbox Code Playgroud)

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

python python-3.x

0
推荐指数
1
解决办法
635
查看次数

标签 统计

python ×2

list-comprehension ×1

python-3.x ×1