2012-12-15 21 views
0

我在氣泡排序後重新執行了程序。將文件中的元素按降序排列而沒有內置函數

def main(): 
try: 
    array=[] 
    file=open(input("Please enter the name of the file you wish to open:")) 
    A =file.read().split() 


    file.close() 



    n = len(A) 
    print ("These following", n,"numbers are in the inputted file:\n", A) 
    for i in range(n): 
     for j in range(1,n-i): 

      if A[j-1] < A[j]: 

       (A[j-1], A[j]) = (A[j],A[j-1]) 
    print("We can now organize it in descending order:\n", A) 

except IOError as e: 
    print("({})".format(e)) 


Output_File = input("Where would you like to save this data?") 
fileObject = open(Output_File, 'a') 
fileObject.write(str(Output_File)+'\n') 
print("Your file is now saved as", Output_File,". \n Have a nice day!") 
fileObject.close() 

如果 == '主要': 主要每3個列表中的號碼()

的問題是,它排序。所以如果我有9個號碼,它會有3個不同的號碼。例如,1 -3 10 6 5 0 3 -5 20將是['6','5','3','20','10','1','0','-5 ','-3']。現在有什麼可能是錯誤的?我做了輸出文件嗎?

+0

那麼,你正在格式化你的代碼示例錯誤的一個。 – millimoose

回答

0

我完成了!我只是想出了一種不同的方式將我的列表變成整數。 它是這樣的:

def main(): 
try: 
    file=open(input("Please enter the name of the file you wish to open:")) 
    A = [] 
    #Here I convert the list to integers to separate as numbers in order to sort later 
    for val in file.read().split(): 
     A.append(int(val)) 
    file.close() 
    n = len(A) 
    print ("These following", n,"numbers are in the inputted file:\n", A) 

    for i in range(n): 
     for j in range(1,n-i): 
      if A[j-1] < A[j]: 
       (A[j-1], A[j]) = (A[j],A[j-1]) #swap 
    print("We can now organize it in descending order:\n", A) 

    Output_File = input("Where would you like to save this data?") 
    fileObject = open(Output_File, 'a') 
    fileObject.write(str(Output_File)+'\n') 
    print("Your file is now saved as",Output_File,".\nHave a nice day!") 
    fileObject.close() 

except IOError as e: 
    print("({})".format(e)) 




if __name__ == '__main__': 
main() 
3

如果你有這樣一行:

x = minimum 

我想你的意思是:

minimum = x 

好像你剛拿到分配順序不正確。在A迭代期間分配變量x將不會產生任何副作用。

編輯

你的問題,因爲我在評論中發現的是,你正在使用的readlines()功能,但只有在你的文件的單行。你真正想要做的是閱讀,然後該行使用split()生成一個列表:

A = file.read().split() 

但是要記住,既然你與「<」進行比較時使用的字符串,你會不會以後得到的數值爲了您的運行你的代碼將得到字典順序。

例如:

輸入:

5 4 14 6 -1 2 0 9 8 7 3 4 -10 200 

輸出:

['-1'] 
['-1', '-10'] 
['-1', '-10', '0'] 
['-1', '-10', '0', '14'] 
['-1', '-10', '0', '14', '2'] 
['-1', '-10', '0', '14', '2', '200'] 
['-1', '-10', '0', '14', '2', '200', '3'] 
['-1', '-10', '0', '14', '2', '200', '3', '4'] 
['-1', '-10', '0', '14', '2', '200', '3', '4', '4'] 
['-1', '-10', '0', '14', '2', '200', '3', '4', '4', '5'] 
['-1', '-10', '0', '14', '2', '200', '3', '4', '4', '5', '6'] 
['-1', '-10', '0', '14', '2', '200', '3', '4', '4', '5', '6', '7'] 
['-1', '-10', '0', '14', '2', '200', '3', '4', '4', '5', '6', '7', '8'] 
['-1', '-10', '0', '14', '2', '200', '3', '4', '4', '5', '6', '7', '8', '9'] 

以上200怎麼不是結束,而是2後談到的通知,讓數值順序,你需要強迫將字符串轉換爲數字數據類型,可能是int。您可以輕鬆地做到這一點,當你使用map功能從文件中讀取的數字:

A = map(int, file.read().split()) 

這將調用分裂返回的每個元素的int投功能之前存儲在A的元素此更改後這是我從你的程序輸出結果:

輸入:

5 4 14 6 -1 2 0 9 8 7 3 4 -10 200 

輸出:

[-10] 
[-10, -1] 
[-10, -1, 0] 
[-10, -1, 0, 2] 
[-10, -1, 0, 2, 3] 
[-10, -1, 0, 2, 3, 4] 
[-10, -1, 0, 2, 3, 4, 4] 
[-10, -1, 0, 2, 3, 4, 4, 5] 
[-10, -1, 0, 2, 3, 4, 4, 5, 6] 
[-10, -1, 0, 2, 3, 4, 4, 5, 6, 7] 
[-10, -1, 0, 2, 3, 4, 4, 5, 6, 7, 8] 
[-10, -1, 0, 2, 3, 4, 4, 5, 6, 7, 8, 9] 
[-10, -1, 0, 2, 3, 4, 4, 5, 6, 7, 8, 9, 14] 
[-10, -1, 0, 2, 3, 4, 4, 5, 6, 7, 8, 9, 14, 200] 
+0

@ user1906407這是否回答您的問題? –

+0

我做了更改和清理程序,但尚未得到我想要的輸出。雖然我沒有收到任何錯誤。以下是我有: new_list = [] 而A: 最小= A [0] 對於x在一個: 如果x <最小值: 最小= X new_list.append(X) new_list .remove(x) print(new_list) break file.close() 我設法分割文件中的數字,但我無法按降序獲取它們! – user1906407

+0

你的輸入文件結構如何?是不同行上的每個元素? –