Python排序,限制结果的打印

Python排序,限制结果的打印,python,sorting,Python,Sorting,我想按日期顺序打印推特日志中的条目。我确实按照日期的顺序对它们进行了排序,但我不知道如何显示最近/前100名列表 代码如下: import codecs with codecs.open('hoge_qdata.tsv','r', 'utf-8') as tweets: tweet_list = tweets.readlines() print tweet_list.pop(0).strip() paired_tweets = sorted([ (int(t.split('\

我想按日期顺序打印推特日志中的条目。我确实按照日期的顺序对它们进行了排序,但我不知道如何显示最近/前100名列表

代码如下:

import codecs

with codecs.open('hoge_qdata.tsv','r', 'utf-8') as tweets:
    tweet_list = tweets.readlines()
    print tweet_list.pop(0).strip()

paired_tweets = sorted([ (int(t.split('\t')[2]), t) for t in tweet_list ], reverse=True)
for p in paired_tweets:
    print p[1].encode('utf-8').strip()

数据文件如下所示

使用python数组切片:

for p in paired_tweets[:100]:
    print p[1].encode('utf-8').strip()

要仅访问列表中的第一个
n
条目,请使用切片:

first_n_entries = my_list[0:n]
例如:

names = ['Dan', 'Joe', 'Greg', 'Molly']
print names[0:3]
>>> ['Dan', 'Joe', 'Greg']

成对tweets[:100]中的p的