I have a data set like this:
Surface Book 2 Review: How Microsoft Won My Five Stars At The Edinburgh Fringe | 0.5047957
Remove the Power PC Care 2018 PUP | 0.44716716
9 Months of Xbox Live (New or Returning Only, otherwise 7 months for $21) $23 | 0.42284298
US probe into Microsoft software sales in Hungary | 0.42226338
我正在尝试根据|
符号后的值对数据进行排序。因此,基本上我想根据具有最高价值的字符串显示整个字符串,并获得前10名。
I tried doing this but I cannot figure out how to order by only that numerical value.
listnew = sorted(Calculatedvalues, key = lambda x : x[1], reverse = True)[:10]
for item in listnew:
print (item)
我的输出是这样的:
38 What is Thinking? #Intelligence | 0.15786803
38 What is Thinking? #Intelligence | 0.15786803
8.31 PF | 0.02431465
️ Hop on, Jerry. Here are 50 points. | 0.25864878
如果我们看到分数基本上比数据集中的分数低,我理解这不是正确的方法,并且我无法仅基于特定零件找到订购方法。
我提到了这些问题中提供的答案,但没有帮助。
从下面提供的答案之一中:我尝试使用上述方法
Calculatedvalues.sort(key = lambda x: x.split("(")[1]))
但我收到此错误
list index out of range
答案 0 :(得分:2)
您很亲密,但是您实际上并未在|
上拆分行,您需要使用split
将每一行转换为列表,然后根据列表的第二项对其进行排序,< / p>
请尝试一下
listnew = sorted(Calculatedvalues, key = lambda x: float(x.split("|")[-1]), reverse=True)[:10]
# output,
# ['Surface Book 2 Review: How Microsoft Won My Five Stars At The Edinburgh Fringe | 0.5047957\n', 'Remove the Power PC Care 2018 PUP | 0.44716716\n', '9 Months of Xbox Live (New or Returning Only, otherwise 7 months for $21) $23 | 0.42284298\n', 'US probe into Microsoft software sales in Hungary | 0.42226338']
答案 1 :(得分:0)
如果您除以'|'
然后转换为float
listnew = sorted(old_list, key=lambda x: float(x.split('|')[1]))