如何用python排序并只输出前几个_如何对100万个数字进行排序,并且只在Python中打印前10个?...

我有一个有一百万个号码的文件。我需要知道我如何能有效地分类,这样它就不会让电脑死机,而且只打印前十名。#!/usr/bin/python3

#Find the 10 largest integers

#Don't store the whole list

import sys

def fOpen(fname):

try:

fd = open(fname,"r")

except:

print("Couldn't open file.")

sys.exit(0)

all = fd.read().splitlines()

fd.close()

return all

words = fOpen(sys.argv[1])

big = 0

g = len(words)

count = 10

for i in range(0,g-1):

pos = i

for j in range(i+1,g):

if words[j] > words[pos]:

pos = j

if pos != i:

words[i],words[pos] = words[pos],words[i]

count -= 1

if count == 0:

print(words[0:10])

我知道这是选择排序,我不确定什么是最好的排序。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值