0

我正在尝试用值格式化表格,但觉得我做得不好。例如,在分割和传递到字符串时必须将整数字典转换为整数。而且 %f 似乎也打印了 0 的负载。我只想要一个带小数点的除法,而不是所有尾随的 0

band1= {'channel1': 10564, 'channel2': 10589, 'channel3': 10612, 'channel4': 10637,'channel5': 10662, 'channel6': 10687,
'channel7': 10712, 'channel8': 10737, 'channel9': 10762, 'channel10': 10787,'channel11': 10812, 'channel12': 10837, }


print '%10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s' % ("Channel 1", "Channel 2", "Channel 3",
"Channel 4", "Channel 5", "Channel 6", "Channel 7","Channel 8", "Channel 9", "Channel 10", "Channel 11", "Channel 12")
print '%10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s' % (band1['channel1'], band1['channel2'], band1['channel3'],
band1['channel4'], band1['channel5'], band1['channel6'], band1['channel7'], band1['channel8'], band1['channel9'], 
band1['channel10'], band1['channel11'], band1['channel12'])
print '%5f %5f %5f %5f %5f %5f %5f %5f %5f %5f %5f %5f' % (int(band1['channel1'])/5.0, int(band1['channel2'])/5.0, int(band1['channel3'])/5.0,
int(band1['channel4'])/5.0, int(band1['channel5'])/5.0, int(band1['channel6'])/5.0, int(band1['channel7'])/5.0, int(band1['channel8'])/5.0, int(band1['channel9'])/5.0, 
int(band1['channel10'])/5.0, int(band1['channel11'])/5.0, int(band1['channel12'])/5.0)

这给出了:

Channel 1  Channel 2  Channel 3  Channel 4  Channel 5  Channel 6  Channel 7  Channel 8  Channel 9 Channel 10 Channel 11 Channel 12
    10564      10589      10612      10637      10662      10687      10712      10737      10762      10787      10812      10837
2112.800000 2117.800000 2122.400000 2127.400000 2132.400000 2137.400000 2142.400000 2147.400000 2152.400000 2157.400000 2162.400000 2167.400000
4

3 回答 3

4
rows = zip(*((key, value, value/5.) for key, value in sorted(band1.items(), 
                                           key= lambda x: int(x[0][7:]))))

print '\n'.join(''.join('{0:<10}'.format(elem) for elem in tup) 
                                               for tup in rows)

出去:

channel1  channel2  channel3  channel4  channel5  channel6  channel7  channel8  channel9  channel10 channel11 channel12
10564     10589     10612     10637     10662     10687     10712     10737     10762     10787     10812     10837    
2112.8    2117.8    2122.4    2127.4    2132.4    2137.4    2142.4    2147.4    2152.4    2157.4    2162.4    2167.4   

分成几块:

cols = [(key, value, value/5.) for key, value in sorted(band1.items(), 
                                        key= lambda x: int(x[0][7:]))]
print cols
# [('channel1', 10564, 2112.8), ('channel2', 10589, 2117.8), ('channel3', 10612, 2122.4), ('channel4', 10637, 2127.4), ('channel5', 10662, 2132.4), ('channel6', 10687, 2137.4), ('channel7', 10712, 2142.4), ('channel8', 10737, 2147.4), ('channel9', 10762, 2152.4), ('channel10', 10787, 2157.4), ('channel11', 10812, 2162.4), ('channel12', 10837, 2167.4)]
rows = zip(*cols)
print rows
# [('channel1', 'channel2', 'channel3', 'channel4', 'channel5', 'channel6', 'channel7', 'channel8', 'channel9', 'channel10', 'channel11', 'channel12'), (10564, 10589, 10612, 10637, 10662, 10687, 10712, 10737, 10762, 10787, 10812, 10837), (2112.8, 2117.8, 2122.4, 2127.4, 2132.4, 2137.4, 2142.4, 2147.4, 2152.4, 2157.4, 2162.4, 2167.4)]
于 2013-04-22T12:08:33.090 回答
1

如果保证每个结果数字只有 1 个小数点且尾随零,那么您可以:

使用字符串格式

>>> i = 2112.800000
>>> print "{0:5.1f}".format(i)
2112.8

或者 :

>>> i = 2112.800000
>>> print "%5.1f" % i
2112.8
于 2013-04-22T12:01:01.027 回答
1

round(your_float, decimal_point)将解决您的问题。

例子:

my_float = 3.5678009

rounded = round(my_float, 2)

print rounded

--> 3.57

于 2013-04-22T11:56:03.907 回答