Ich bin eine Übung wie folgt:Unterschied zwischen 2 Stück Python-Code
# B. front_x
# Given a list of strings, return a list with the strings
# in sorted order, except group all the strings that begin with 'x' first.
# e.g. ['mix', 'xyz', 'apple', 'xanadu', 'aardvark'] yields
# ['xanadu', 'xyz', 'aardvark', 'apple', 'mix']
# Hint: this can be done by making 2 lists and sorting each of them
# before combining them.
Probenlösung:
def front_x(words):
listX = []
listO = []
for w in words:
if w.startswith('x'):
listX.append(w)
else:
listO.append(w)
listX.sort()
listO.sort()
return listX + listO
meine Lösung:
def front_x(words):
listX = []
for w in words:
if w.startswith('x'):
listX.append(w)
words.remove(w)
listX.sort()
words.sort()
return listX + words
wie ich meine Lösung getestet Das Ergebnis ist ein bisschen komisch. Hier ist der Quellcode mit meiner Lösung: http://dl.dropbox.com/u/559353/list1.py. Vielleicht möchten Sie es ausprobieren.
Haben Sie eine spezielle Frage, außer "Was ist falsch mit meinem Code"? – Bobby
-1: "Das Ergebnis ist ein bisschen komisch". Vage und schwer zu beantworten. Bitte geben Sie etwas Spezifisches an, das Sie reparieren möchten. –