列出Python Quanti

2024-10-05 13:03:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我在做一个分位数的问题,我需要做些类似的事情

间隔时间:

150-155
155-160
160-165
165-170
170-175
175-180
180-185

^{pr2}$

这些是变量 我需要这个因为我在做一个表的间隔

>> width = range/number_of_intervals
>> while inferior_limit <= superior_limit
# there is my problem
>> inferior_limit += width
>> print inferior_limit

Tags: ofnumber间隔israngewidth事情there
2条回答

这是你的意思吗?在

>>> inf, sup, delta = 150, 185, 5
>>> print '\n'.join('{}-{}'.format(x, x + delta) for x in xrange(inf, sup, delta))
150-155
155-160
160-165
165-170
170-175
175-180
180-185
>>> start, stop, step = 150, 185, 5
>>> r = range(start, stop + 1, step) # You can use xrange on py 2 for greater efficiency
>>> for x, y in zip(r, r[1:]):
        print '{0}-{1}'.format(x, y)


150-155
155-160
160-165
165-170
170-175
175-180
180-185

更有效的方法是使用itertools成对配方。在

^{pr2}$

这里还有一个使用itertools.starmap的解决方案,因为从来没有人使用过它!在

from itertools import starmap
print '\n'.join(starmap('{0}-{1}'.format, pairwise(r)))

相关问题 更多 >

    热门问题