2014-07-08 41 views
0

我想按日期顺序打印来自Twitter日志的条目。我没有管理它们的日期进行排序,但我无法弄清楚如何显示近期/前100名Python排序,限制结果打印

下面的代码:下面提供

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() 

数据文件... http://web.sfc.keio.ac.jp/~t12102ti/isc/tweetsample.zip

+3

'用于paired_tweets P [100]'? –

回答

1

使用Python阵列片:

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

非常感谢你! – user3817269

0

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

first_n_entries = my_list[0:n] 

例如:

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

我看到谢谢你的好解释! – user3817269