我正在尝试打印一个字典(实际上是defaultdict
),其中键是版本号(格式为6.0
或6.1.2
),值为浮点数。< / p>
# 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&gt; 7.0.1&gt; 6.1.4&gt; 6.1等等)?
答案 0 :(得分:5)
我认为@dornhege首先发现了主要问题:key=d.get
意味着你要按值排序,而不是键。
我会利用stdlib包distutils
来自动处理版本内容:
>>> sorted(firmware_percents, key=distutils.version.StrictVersion, reverse=True)
['7.0.3', '7.0.2', '7.0.1', '7.0', '6.1.4', '6.1.3', '6.1.2', '6.1.1', '6.1', '6.0.2', '6.0.1', '6.0', '5.1.1', '5.1', '5.0.1', '5.0']
>>> sorted(firmware_percents, key=distutils.version.LooseVersion, reverse=True)
['7.0.3', '7.0.2', '7.0.1', '7.0', '6.1.4', '6.1.3', '6.1.2', '6.1.1', '6.1', '6.0.2', '6.0.1', '6.0', '5.1.1', '5.1', '5.0.1', '5.0']
这比.
拆分更有优势,因为处理7.0.2rc1
之类的事情会更聪明。
答案 1 :(得分:2)
将版本号转换为整数列表以进行排序:
def pretty_print(d):
for k in sorted(d, key=lambda x: x.split("."), reverse=True):
print("{0}: {1:.1f}".format(k, d[k]))
但使用operator.methodcaller
可能会更有效率。
from operator import methodcaller
def pretty_print(d):
for k in sorted(d, key=methodcaller('split', '.'), reverse=True):
print("{0}: {1:.1f}".format(k, d[k]))
答案 2 :(得分:1)
在Python中,字典不可排序 - 而使用字典“get”方法作为排序键的食谱是按字典值排序 - 而不是按键排序。
所以,当你致电for k in sorted(d, key=d.get, reverse=True)
时
会发生什么是字典中的每个键都被传递给dictionairy的“get”方法 - 而不是检索值。
如果您想按键排序,则根本不需要将任何值传递给“key”。做就是了:
for k in sorted(d, reverse=True):
然而,像“10.1”这样的版本将首先出现在“2.0”之上,因为它是一个字符串比较 - 将版本号拆分为“。”并将每个部分转换为数字将产生正确的比较:
for k in sorted(d, key=lambda x: tuple(int(n) for n in x.split(".")) , reverse=True):