0

値を使用してテーブルをフォーマットしようとしていますが、やり方が悪いと感じています。たとえば、分割して文字列に渡すときに、int の辞書を int に変換する必要があります。また、 %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

round(your_float, decimal_point)あなたの問題を解決します。

例:

my_float = 3.5678009

rounded = round(my_float, 2)

print rounded

--> 3.57

于 2013-04-22T11:56:03.907 に答える
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 に答える