python统计十大 [英] python statistic top 10

查看:93
本文介绍了python统计十大的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用python 2.6

using python 2.6

我有大文本文件。
以下是前3个条目,但有超过50个用户需要检查。

I have large text file. Below are the first 3 entries, but there are over 50 users I need to check.

html_log:jeff 1153.3 1.84 625:54 1 2 71 3 2 10 7:58 499 3 5 616:36 241 36 html_log:fred 28.7 1.04 27:34 -10 18 13 0:48 37 18 8 -3.63 html_log:bob 1217.1 1.75 696:48 1 5 38 6 109 61 14:42 633 223 25 435:36 182 34 ... continues

I需要在这种情况下找到用户名,在html_log:标签之后的文本

I need to beable to find the username in this case the text after the "html_log:" tags

我还需要评级(用户名旁边的第一组值)

I also need the rating (first set of values next to the username.)

输出将检查整个txt文件,并输出前十名最高评分的玩家。

Output would check the entire txt file and output the top 10 highest rated players.

请注意,不总是16套价值观,一些包含的数量远远少于。

Please note that there are not always 16 sets of values, some contain far less.

生产:

bob 1217.1
jeff 1153
fred 28.7


推荐答案

在这种情况下,我实际上会使用正则表达式。

In this case I would actually use a regular expression.

只要考虑 html_log:作为记录开始标记,下一部分直到空格为名。下一部分是分数,您可以将其转换为浮动比较:

Just consider html_log: as a record start marker, the next part up until a whitespace is the name. The next part after it is the score, which you can convert to float for comparison:

s = "html_log:jeff 1153.3 1.84 625:54 1 2 71 3 2 10 7:58 499 3 5 616:36 241 36 html_log:fred 28.7 1.04 27:34 -10 18 13 0:48 37 18 8 -3.63 html_log:bob 1217.1 1.75 696:48 1 538 6 109 61 14:42 633 223 25 435:36 182 34"
pattern = re.compile("html_log:(?P<name>[^ ]*) (?P<score>[^ ]*)")
print sorted(pattern.findall(s), key=lambda x: float(x[1]), reverse=True)

# [('bob', '1217.1'), ('jeff', '1153.3'), ('fred', '28.7')]

这篇关于python统计十大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆