2014-12-04 73 views
1

這裏是我的代碼:程序不寫列表按字母順序排列的輸出到文件

def typeMovie(): 
    import os 
    os.chdir("/Users/Name/Desktop/Data Sets") 

    input_file = open("MoviesTotalEarnings.txt", "r") 
    output_file = open("MoviesTotalEarningsType.txt", "w") 

    allItems = [] 

    for line in input_file: 
     data = line.split("\t") 

     allItems.append(data) 

    allItems.sort() 

    for data in allItems: 
     output_file.write("{}\n".format(data[2])) 

    input_file.close() 
    output_file.close() 

typeMovie() 

下面是輸出:

Best Picture 
Biggest Gross 
Biggest Gross 
Best Picture 
Sundance 
Sundance 
Series 
Series 
Series 
Series 
Series 
Best Picture 
Biggest Gross 
Sundance 
Biggest Gross 
Biggest Gross 
Sundance 
Biggest Gross 
Best Picture 
Best Picture 
Series 
Series 
Biggest Gross 
Series 
Sundance 
Biggest Gross 
Biggest Gross 
Sundance 
Sundance 
Best Picture 
Series 
Series 
Series 
Biggest Gross 
Series 
Series 
Series 
Biggest Gross 
Biggest Gross 
Sundance 
Sundance 
Best Picture 
Sundance 
Sundance 
Best Picture 
Best Picture 
Best Picture 
Biggest Gross 
Biggest Gross 
TYPE 

以上輸出不正確的輸出我在尋找。顯然,它不是按字母順序輸出到輸出文件,我不知道如何解決它。

預期的輸出應沿着這些路線:

Best Picture 
Best Picture 
Best Picture 
Biggest Gross 
Biggest Gross 
Series 
Series 
Series 
Sundance 
Sundance 

對於上述輸出唯一的一點是,他們將在文件中這些話更多的實例,但它只是一個例子。

這裏的 「MoviesTotalEarnings」 的實際文本文件:

NUMBER MOVIE TYPE TOTAL 
1 A Beautiful Mind Best Picture 170.71 
2 American Beauty Best Picture 130.06 
3 Batman Biggest Gross 251.19 
4 Beverly Hills Cop Biggest Gross 234.76 
5 Chicago Best Picture 170.69 
6 Crash Best Picture 55.33 
7 "Departed, The" Best Picture 133.31 
8 "Empire Strikes Back, The" Biggest Gross 290.27 
9 ET Biggest Gross 435.11 
10 Forrest Gump Biggest Gross 329.69 
11 Ghost Busters Biggest Gross 238.63 
12 Gladiator Best Picture 187.68 
13 Gods and Monsters Sundance 6.45 
14 "Good Girl, The" Sundance 14.02 
15 Harry Potter 1: Sorcerer's Stone Series 317.56 
16 Harry Potter 2: Chamber of Secrets Series 261.99 
17 Harry Potter 3: Prisoner of Azkeban Series 249.54 
18 Harry Potter 4: Goblet of Fire Series 290.01 
19 Harry Potter 5: Order of the Phoenix Series 292 
20 Home Alone Biggest Gross 285.76 
21 In the Company of Men Sundance 2.88 
22 Independence Day Biggest Gross 306.17 
23 Jurassic Park Biggest Gross 357.07 
24 "Last Mimzy, The" Sundance 21.47 
25 "Lion King, The" Biggest Gross 312.86 
26 Lord of the Rings: The Return of the King Best Picture 377.03 
27 Million Dollar Baby Best Picture 100.42 
28 Pirates 1: Curse of the Black Pearl Series 305.41 
29 Pirates 2: Dead Man's Chest Series 423.32 
30 Pirates 3: At World's End Series 309.4 
31 Quinceanera Sundance 1.69 
32 Raiders of the Lost Ark Biggest Gross 242.37 
33 Return of the Jedi Biggest Gross 309.21 
34 "Road Home, The" Sundance 1.28 
35 Run Lola Run Sundance 7.27 
36 Shakespeare in Love Best Picture 100.32 
37 Shrek Series 267.65 
38 Shrek 2 Series 436.72 
39 Shrek the Third Series 321.01 
40 Spider-Man Series 403.71 
41 Spider-Man 2 Series 373.52 
42 Spider-Man 3 Series 336.53 
43 Star Wars Biggest Gross 461 
44 Star Wars: Phantom Menace Biggest Gross 431.09 
45 Super Size Me Sundance 11.53 
46 Thirteen Sundance 4.6 
47 Titanic Best Picture 600.79 
48 "Upside of Anger, The" Sundance 18.76 
49 You Can Count on Me Sundance 9.18 
+0

「MoviesTotalEarnings.txt」如何顯示?我問,因爲'allItems.sort()'是排序列表的列表。不是字符串列表。 – Marcin 2014-12-04 05:06:44

+0

我做了答案。想想,你需要指定一個將用於排序的鍵。 – Marcin 2014-12-04 05:16:03

回答

3

我認爲你需要添加關鍵是你的排序。因此,要對第三列進行排序,您需要

allItems.sort(key=lambda v: v[2]) 
+0

這工作完美。謝謝。 – TZK203 2014-12-04 05:19:00

2

allItems.sort(key=operator.itemgetter(2))

相關問題