2014-10-06 59 views
-3

所以我知道如何導入texfile和排序的數字,如:Python的 - 排序

1 
6 
4 
6 
9 
3 
5 

但我不知道如何排序,看起來像一個數據:

Merchant_9976 20122 
Merchant_9977 91840 
Merchant_9978 92739 
Merchant_9979 97252 
Merchant_9980 76885 
Merchant_9981 67835 
Merchant_9982 42201 
Merchant_9983 47463 

這裏的到目前爲止我的代碼

import time 


def sort_slow(seq): 
    """ 
    :param seq: 
    :return: 
    """ 
    for i in range(1, len(seq)): 
     j = i 
     while j > 0 and seq[j - 1] > seq[j]: 
      seq[j - 1], seq[j] = seq[j], seq[j - 1] 
      j -= 1 
    return seq 


def main(): 
    fileName = str(input('Please enter a filename: ')) 
    file = open(fileName) 
    sort1 = [] 
    for lines in file: 
     sort1.append(int(lines.strip())) 
    #a = [3, 5, 2, 1, 10] 
    starting = time.clock() 
    print(starting) 
    sort_slow(sort1) 
    print(sort1) 
    #print(sort_slow([a])) 
    #print(sort_slow(a)) 
    elapsed = time.clock() - starting 
    print(elapsed) 


main() 

回答

0
lines = file.readlines() 
ar = map(lambda x: x.split(), lines) 
ar = ar.sort(key=lambda x: x[1]) 

ar包含在第二列中排序的文件中的所有行。