独立阵列相互干扰?

时间:2014-01-18 07:33:43

标签: python sorting

有两列的给定数据集: 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******")

然而,在第二次排序后, sName sCode 的第一次排序结果自行更改。我从来没有手动改变它。

BEFORE(第一次排序)

    **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*******

AFTER(第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

谁能告诉我这怎么可能发生?

1 个答案:

答案 0 :(得分:1)

在Python中传递的赋值和参数不会创建对象的副本。当您对列表进行冒泡时,传递到冒泡排序的列表与内存中的精确列表对象一样,是冒泡排序传回的列表。 SurnamesNamecName是完全相同的对象,当您执行第二次冒泡排序时,您可以修改sNamesCode,而不是创建独立的,排序清单。

如果要复制列表,则必须明确地执行此操作。这是一个浅表副本:

new_list = original[:]

new_list将是一个包含相同对象original的新列表。

这是一份深刻的副本:

import copy
new_list = copy.deepcopy(original)

new_list将是一个新列表,其中包含对象original的深层副本。 (这有时深;例如,如果你有一个列表列表,你有时不想复制内部列表中的对象。)

最后,我想指出你的初始化代码非常冗长。您可以使用列表文字而不是创建一个完整的零列表并分别分配每个元素:

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