String-Vergleich in Python
Ich habe versucht, das strcmp von c in Python zu replizieren. Ich habe das erste Programm eingegeben und es hat funktioniert, aber das zweite scheint auch zu funktionieren. Bitte erläutern Sie das zweite. Ich habe nur erwartet, dass == funktioniert, aber>, <scheint zu funktionieren. Woher weiß Python, dass mit <,> die Länge eines Strings ohne len () gemeint ist?
def strcmp(str1,str2):
if(len(str1) == len(str2)):
return 0
if(len(str1) > len(str2)):
return 1
if(len(str1) < len(str2)):
return -1
print strcmp("ashsih","aapam")
vs
def strcmp(str1,str2):
if(str1 == str2):
return 0
if(str1 > str2):
return 1
if(str1 < str2):
return -1
print strcmp("ashsih","aapam")