Hallo ,
Ich soll in einem mehrdimensionalen array sortieren und frage mich nun wie ich nach dem 2 element(1995, 3, 24) in jedem array sortieren kann.
Mein Lehrer hat folgendes Programm zur Verfügung gestellt.
def selection_sort(a):
for j in range(len(a)-1):
minimum = j #assume that element with j index is minimum
for i in range(j+1, len(a)): #compare it with unsorted elements(we leave the sorted elements behind)
if(a<a[minimum]):
minimum = i #update minimum index with the smaller element
a[j],a[minimum]=a[minimum],a[j] #swap the elements that are assumed minimum and actual minimum found in unsorted list
print("after selection sort:")
print(a)
L= [["Berlin", "1995", "31"],
["Nrw", "3", "6"],
["Bayern", "24", "28"]]
selection_sort(L)
In mehrdimensionalen array sortieren
Eingerückt wird immer mit 4 Leerzeichen pro Ebene, keine Tabs.
`if` ist keine Funktion, die Klammern daher überflüssig.
Die Ausgabe sollte nicht in der sort-Funktion stattfinden.
An welcher Stelle wird was miteinander verglichen?
Und wie würde man an dieser Stelle das zweite Element herausfischen?
`if` ist keine Funktion, die Klammern daher überflüssig.
Die Ausgabe sollte nicht in der sort-Funktion stattfinden.
Code: Alles auswählen
def selection_sort(a):
for j in range(len(a) - 1):
minimum = j #assume that element with j index is minimum
for i in range(j + 1, len(a)): #compare it with unsorted elements(we leave the sorted elements behind)
if a[i] < a[minimum]:
minimum = i #update minimum index with the smaller element
a[j], a[minimum] = a[minimum], a[j] #swap the elements that are assumed minimum and actual minimum found in unsorted list
elements = [
["Berlin", "1995", "31"],
["Nrw", "3", "6"],
["Bayern", "24", "28"],
]
selection_sort(elements)
print("after selection sort:")
print(elements)
Und wie würde man an dieser Stelle das zweite Element herausfischen?