2014-01-18 82 views
0

有兩列的給定數據集:EmployeeCode姓氏。 格式是這樣的:獨立陣列互相干擾?

EmployeeCode[1] = "L001" 
Surname[1] = "Pollard" 
EmployeeCode[2] = "L002" 
Surname[2] = "Wills" 
... 

什麼,我試圖做的是按照字典順序爲每一列,以便以後幫助執行二進制搜索的排序。

這是我的代碼:

#data set 
EmployeeCode, Surname = [0]*33, [0]*33 
EmployeeCode[1] = "L001" 
Surname[1] = "Pollard" 
EmployeeCode[2] = "L002" 
Surname[2] = "Wills" 
EmployeeCode[3] = "L007" 
Surname[3] = "Singh" 
EmployeeCode[4] = "L008" 
Surname[4] = "Yallop" 
EmployeeCode[5] = "L009" 
Surname[5] = "Adams" 
EmployeeCode[6] = "L013" 
Surname[6] = "Davies" 
EmployeeCode[7] = "L014" 
Surname[7] = "Patel" 
EmployeeCode[8] = "L021" 
Surname[8] = "Kelly" 
EmployeeCode[9] = "S001" 
Surname[9] = "Ong" 
EmployeeCode[10] = "S002" 
Surname[10] = "Goh" 
EmployeeCode[11] = "S003" 
Surname[11] = "Ong" 
EmployeeCode[12] = "S004" 
Surname[12] = "Ang" 
EmployeeCode[13] = "S005" 
Surname[13] = "Wong" 
EmployeeCode[14] = "S006" 
Surname[14] = "Teo" 
EmployeeCode[15] = "S007" 
Surname[15] = "Ho" 
EmployeeCode[16] = "S008" 
Surname[16] = "Chong" 
EmployeeCode[17] = "S009" 
Surname[17] = "Low" 
EmployeeCode[18] = "S010" 
Surname[18] = "Sim" 
EmployeeCode[19] = "S011" 
Surname[19] = "Tay" 
EmployeeCode[20] = "S012" 
Surname[20] = "Tay" 
EmployeeCode[21] = "S013" 
Surname[21] = "Chia" 
EmployeeCode[22] = "S014" 
Surname[22] = "Tan" 
EmployeeCode[23] = "S015" 
Surname[23] = "Yeo" 
EmployeeCode[24] = "S016" 
Surname[24] = "Lim" 
EmployeeCode[25] = "S017" 
Surname[25] = "Tan" 
EmployeeCode[26] = "S018" 
Surname[26] = "Ng" 
EmployeeCode[27] = "S018" 
Surname[27] = "Lim" 
EmployeeCode[28] = "S019" 
Surname[28] = "Toh" 
EmployeeCode[29] = "N011" 
Surname[29] = "Morris" 
EmployeeCode[30] = "N013" 
Surname[30] = "Williams" 
EmployeeCode[31] = "N016" 
Surname[31] = "Chua" 
EmployeeCode[32] = "N023" 
Surname[32] = "Wong" 


#sort based on value of main array 
def bubble_sort(main, second): 
    sort = True 
    passed = len(main)-1 
    while sort: 
     sort = False 
     i = 2 
     while i<= passed: 
      #print(main[i],main[i-1],i) 
      if main[i] < main[i-1]: 
       main[i], main[i-1] = main[i-1], main[i] 
       second[i], second[i-1] = second[i-1], second[i] 
       sort = True 
      i += 1 
     passed -= 1 
    return main,second 



#main 
#prepare sorted array for binary search 
#for search by surname, sort according to surname 
sName,sCode = bubble_sort(Surname,EmployeeCode) 


print("**BEFORE******") 
for k in range(0,33): 
    print(sName[k],sCode[k]) 
print("*BEFORE*******") 

#for search by ECode, sort according to ECode 
cCode,cName = bubble_sort(EmployeeCode, Surname) 


print("**AFTER******") 
for k in range(0,33): 
    print(sName[k],sCode[k]) 
print("**AFTER******") 

然而,第二次分揀後,第一時間SNAMESCODE排序結果只是自己改變。我從來沒有手動改變它。

之前(第1排序)

**BEFORE****** 
0 0 
Adams L009 
Ang S004 
Chia S013 
Chong S008 
Chua N016 
Davies L013 
Goh S002 
Ho S007 
Kelly L021 
Lim S016 
Lim S018 
Low S009 
Morris N011 
Ng S018 
Ong S001 
Ong S003 
Patel L014 
Pollard L001 
Sim S010 
Singh L007 
Tan S014 
Tan S017 
Tay S011 
Tay S012 
Teo S006 
Toh S019 
Williams N013 
Wills L002 
Wong S005 
Wong N023 
Yallop L008 
Yeo S015 
*BEFORE******* 

後(第2分選,見最後4項)

**AFTER****** 
0 0 
Pollard L001 
Wills L002 
Singh L007 
Yallop L008 
Adams L009 
Davies L013 
Patel L014 
Kelly L021 
Morris N011 
Williams N013 
Chua N016 
Wong N023 
Ong S001 
Goh S002 
Ong S003 
Ang S004 
Wong S005 
Teo S006 
Ho S007 
Chong S008 
Low S009 
Sim S010 
Tay S011 
Tay S012 
Chia S013 
Tan S014 
Yeo S015 
Lim S016 
Tan S017 
Lim S018 
Ng S018 
Toh S019 

誰能告訴我怎麼會發生這種情況?

+0

什麼是您的'EmployeeCode'和'Surname'對象? – mhlester

+0

爲什麼不直接使用內置的'sort'? –

回答

1

在Python中傳遞的賦值和參數不會創建對象的副本。當您將列表泡泡時,您傳入泡泡排序的列表與內存中排列的泡泡列表返回的列表完全相同。 Surname,sNamecName是完全相同的對象,當您執行第二次冒泡排序時,您修改sNamesCode而不是創建獨立的排序列表。

如果你想複製一個列表,你必須明確地這樣做。這是一個淺拷貝:

new_list = original[:] 

new_list將包含original包含在同一對象的新名單。

這是一個深刻的副本:

import copy 
new_list = copy.deepcopy(original) 

new_list將包含original所包含對象的深層副本一個新的列表。 (這有時是深;例如,如果你有一個列表的列表,你有時候不想複製這些對象的內部名單內)

最後,我想指出的是您初始化代碼非常冗長。您可以使用列表文字而不是創建一個滿零的列表並分別分配每個元素:

EmployeeCode = [ 
    'Pollard', 
    'Wills', 
    ... 
]