2014-02-13 2 views
0

는 결국 다음과 같습니다 그래서 내가리스트로 텍스트 파일을 넣어했다이 assignement 있습니다 목록 버블 정렬과 정렬 (목록리스트)

[["Afghanistan",647500.0,25500100],["Albania",28748.0,2821977],...,["Zimbabwe",390580.0,12973808]] 

은 그래서 목록의 목록이

을 정의하는 함수에 의해 readcountries() 나는이 목록을 버블 정렬 (할당에 의해 요구됨) 목록의 목록에있는 매개 변수 중 하나 (즉, 국가 이름 뒤의 두 번째 숫자 인 인구)에 따라 정렬하려고합니다).

이것은 내가 지금까지

def bubblesort(): 
    Countries = readcountries() 
    for i in range(0,len(Countries)): 
     madeSwap = False 
     for j in range (0,len(Countries)-(i+1)): 
      if Countries[j][2] > Countries[j+1][2]: 
       temp = Countries[j+1] 
       Countries[j+1][2] = Countries[j][2] 
       Countries[j] = temp 
       madeSwap = True 
      if not madeSwap: 
       return 

을하지만 어떤 이유로이로 분류하고 심지어 문제가 후 정렬 된 목록을 볼 수있는 모든 종류의를 얻을 해달라고 것입니다. 어떤 도움이

그리고 수익을 외부에서이

if not madeSwap: 
       return 

를 호출해야 당신이 또한 Countries[j+1] = Countries[j]

로 교체해야

+2

글쎄, 처음에는 '국가'를 반환하는 것이 좋습니다. – 2rs2ts

+2

또한,'[2]'뿐만 아니라 전체 항목을 바꾸고 싶습니다. – Eric

답변

0

문제는 여기 Countries[j+1][2] = Countries[j][2] 입니다 환영합니다 목록

def bubblesort(): 
     Countries = [["Afghanistan",647500.0,3],["Albania",28748.0,1],["Zimbabwe",390580.0,2]] 
     for i in range(0,len(Countries)): 
      madeSwap = False 
      for j in range (0,len(Countries)-(i+1)): 
       if Countries[j][2] > Countries[j+1][2]: 
        temp = Countries[j+1] 
        Countries[j+1] = Countries[j] 
        Countries[j] = temp 
        madeSwap = True 
      if not madeSwap: 
       return Countries 
     return Countries 

>>> bubblesort() 
[['Afghanistan', 647500.0, 1], ['Zimbabwe', 390580.0, 2], ['Albania', 28748.0, 3]]