defaultdict
キーがバージョン番号 (6.0
またはの形式6.1.2
) で、値が floatである辞書 (実際には ) を出力しようとしています。
# Parse file contents
firmware_percents = defaultdict(float)
with open('test.csv', 'r') as file:
reader = csv.DictReader(file)
for row in reader:
# Don't care about iphone vs ipad
# Must be a string - StrictVersion is apparently unhashable
rev = row["Firmware Version"].split(" ")[2]
# Dump extra spaces, the less than (assume .1%), and % sign
percent = float(row["% of Sessions"].strip().lstrip("<").strip("%"))
firmware_percents[rev] += percent
def pretty_print(d):
for k in sorted(d, key=d.get, reverse=True):
print("{0}: {1:.1f}".format(k, d[k]))
print("All versions:")
pretty_print(firmware_percents)
ただし、これを行うと、一部のバージョンが順不同で印刷されます。
All versions:
7.0: 44.2
7.0.2: 25.7
6.1.3: 14.2
6.1.4: 5.5
7.0.1: 3.2
6.1: 2.7
# You get the point
この入力ファイルの使用:
Firmware Version,Sessions,% of Sessions
" iPhone 5.0.1 ",20," <0.1% "
" iPhone 6.0 ",26," 0.1% "
" iPhone 5.1.1 ",69," 0.3% "
" iPhone 5.1 ",2," <0.1% "
" iPhone 7.0 ",7401," 31.5% "
" iPhone 6.1 ",337," 1.4% "
" iPhone 6.1.3 ",2193," 9.3% "
" iPhone 6.1.2 ",84," 0.4% "
" iPhone 7.0.1 ",747," 3.2% "
" iPhone 7.0.2 ",4619," 19.7% "
" iPhone 6.0.1 ",37," 0.2% "
" iPhone 6.0.2 ",1," <0.1% "
" iPhone 6.1.4 ",1281," 5.5% "
" iPad 5.0 ",4," <0.1% "
" iPad 5.1 ",100," 0.4% "
" iPad 5.1.1 ",545," 2.3% "
" iPad 6.0 ",16," <0.1% "
" iPad 6.1 ",305," 1.3% "
" iPhone 7.0.3 ",1," <0.1% "
" iPhone 6.1.1 ",1," <0.1% "
" iPad 7.0 ",2979," 12.7% "
" iPad 6.0.1 ",100," 0.4% "
" iPad 6.1.3 ",1139," 4.9% "
" iPad 6.0.2 ",5," <0.1% "
" iPad 6.1.2 ",65," 0.3% "
" iPad 7.0.2 ",1404," 6.0% "
を試してみましpprint
たが、正しい順序でソートされますが、フロートはフォーマットされません (したがって、14.5999999996 のような数値になります)。また、defaultdict(<class 'float'>, {'5': 3.3, '6': 24.2, '7': 73.2})
メジャーバージョンのみを実行しようとすると、奇妙に出力されることがあります。
これらがフォーマットされたパーセンテージでバージョン順に印刷されるようにするにはどうすればよいですか?
順番は、メジャー、マイナー、ビルド/スーパーマイナー (7.0.2 > 7.0.1 > 6.1.4 > 6.1 など) でソートされることを意味しますか?