2017-05-18 8 views
-1

나는 사전을 정렬하기위한 현재 알고리즘을 가지고 있지만 4 개 이상의 요소를 비교하면 작동하지 않을 것입니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?거품 정렬을 사용하여 파이썬에서 사전을 정렬하는 방법

database={ 
    0:['Ninna','Layug','201504584','09954895032','Quezon City','[email protected]','18','02/21/1999'], 
    1:['Yela','Gregorio','201506070','09984548540','UP Diliman','[email protected]','19','04/18/1999'], 
    2:['Denise','Gregorio','201506070','09984548540','UP Diliman','[email protected]','19','04/18/1999'], 
    3:['Alia','Layug','201504584','09954895032','Quezon City','[email protected]','18','02/21/1999'], 
    4:['Keeno','Layug','201504584','09954895032','Quezon City','[email protected]','18','02/21/1999'] 
} 
profiles=['0','1','2','3','4'] 

for x in range(len(profiles)): 
    print(profiles) 
    for j in range(len(profiles)-1-x): 
     v1=database[j][0] 
     v2=database[j+1][0] 

     if v1>v2: 
      sorted=False 
      temp=profiles[j+1] 
      profiles[j+1]=profiles[j] 
      profiles[j]=temp 
for x in profiles: 
    print(database[int(x)][0],",",database[int(x)][1]) 
+0

'sort'를 사용하는 대신 고유 한 정렬 알고리즘을 구현하는 특별한 이유가 있습니까? – Kevin

+1

가능한 [Bubble Sort Homework] (http://stackoverflow.com/questions/895371/bubble-sort-homework) 중복 가능 –

답변

0

코드에 몇 가지 변경을했습니다.)

v1=database[int(profiles[j])][0] 
v2=database[int(profiles[j-1])][0] 

대신

v1=database[j][0] 
v2=database[j+1][0] 

j는 프로파일의 인덱스하지만 우리가 필요로 사전에 열쇠이다 프로파일의 실제 값이있을 것이기 때문에

.

그런 다음 거품 j와 j + 1을 사용했으며 0에서 len (프로필) -1 범위의 값만 고려했습니다. 이 -1은 마지막 값을 놓치게합니다. 1 렌으로 (프로파일) 및 사용 버블 J와 J-1 여기

에 이르기까지 그래서 내가 생각 한 값은

database={ 
    0:['Ninna','Layug','201504584','09954895032','Quezon City','[email protected]','18','02/21/1999'], 
    1:['Yela','Gregorio','201506070','09984548540','UP Diliman','yelagreg[email protected]','19','04/18/1999'], 
    2:['Denise','Gregorio','201506070','09984548540','UP Diliman','[email protected]','19','04/18/1999'], 
    3:['Alia','Layug','201504584','09954895032','Quezon City','[email protected]','18','02/21/1999'], 
    4:['Keeno','Layug','201504584','09954895032','Quezon City','[email protected]','18','02/21/1999'] 
} 
profiles=['0','1','2','3','4'] 

for x in range(len(profiles)): 
    print(profiles) 
    for j in range(1,len(profiles)-x): 
     v1=database[int(profiles[j])][0] 
     v2=database[int(profiles[j-1])][0] 

     if v1>v2: 
      sorted=False 
      temp=profiles[j-1] 
      profiles[j-1]=profiles[j] 
      profiles[j]=temp 
for x in profiles: 
    print(database[int(x)][0],",",database[int(x)][1]) 

희망이 출력과 같이 당신

작동 전체 코드입니다 다음과 같습니다.

['0', '1', '2', '3', '4'] 
['1', '0', '2', '4', '3'] 
['1', '0', '4', '2', '3'] 
['1', '0', '4', '2', '3'] 
['1', '0', '4', '2', '3'] 
Yela , Gregorio 
Ninna , Layug 
Keeno , Layug 
Denise , Gregorio 
Alia , Layug 
+0

안녕하세요! 나는 이것을 어떻게 오름차순으로 하는가? –

+0

if v1> v2 : to v1 George

+0

정렬 방식에 따라 사전에있는 데이터의 순서를 영구히 변경하고 싶습니다. 어떻게해야합니까? 예를 들어 정렬 순서는 텍스트 파일에 저장하는 방법입니다. –

0

이 두 줄은 잘못되었습니다. 해당 인덱스에 저장된 프로필 키 대신 의 인덱스를 range()에 사용하고 있습니다. 스와핑에 관계없이 항상 동일한 요소를 비교하므로 작업이 중단됩니다.

v1=database[j][0] 
v2=database[j+1][0] 

그들은이 같아야합니다

v1 = database[int(profiles[j])][0] 
v2 = database[int(profiles[j+1])][0] 

다음은 첫 번째 패스를 실행 한 결과입니다. 상위 v1/v2는 잘못된 값이고 맨 아래의 값은 올바른 값입니다.

profiles[j], profiles[j+1] = profiles[j+1], profiles[j] 

는 여기에 더 나은 알고리즘입니다 :

swapped = False 
while not swapped: 
    swapped = True 
    print(profiles) 
    for j in range(len(profiles) - 1): 
     v1 = database[int(profiles[j])][0] 
     v2 = database[int(profiles[j+1])][0] 
     if v1 > v2: 
      swapped = False 
      profiles[j], profiles[j+1] = profiles[j+1], profiles[j] 

그리고 여기에 표준 방법을 사용하여이를 수행하는 방법은 다음과 같습니다

profiles = list(sorted(profiles, key=lambda x: database[int(x)])) 
를 파이썬에서 교환하는 방법 또한
x = 0                  
print(['0', '1', '2', '3', '4'])           
j = 0  | j = 1    | j = 2    | j = 3    
v1 = 'Ninna' | v1 = 'Yela'  | v1 = 'Denise'  | v1 = 'Alia'  
v2 = 'Yela' | v2 = 'Denise'  | v2 = 'Alia'  | v2 = 'Keeno'  
      |     |     |     
v1 = 'Ninna' | v1 = 'Yela'  | v1 = 'Yela'  | v1 = 'Yela'  
v2 = 'Yela' | v2 = 'Denise'  | v2 = 'Alia'  | v2 = 'Keeno'  
      |     |     |     
      |     |     |     
      | sorted = False | sorted = False | sorted = False  
      | temp = '2'  | temp = '3'  | temp = '4'   
      | profiles[2] = '1' | profiles[3] = '1' | profiles[4] = '1' 
      | profiles[1] = '2' | profiles[2] = '3' | profiles[3] = '4' 

이있다
+0

안녕하세요! 내 사전에서 정렬 된 방식으로 데이터의 순서를 변경하려면 어떻게해야합니까? 내 앞의 코드는 정렬 후에 데이터베이스 안의 배열을 바꾸지 않기 때문에 {}. –

관련 문제