我是python的新手 我正在寻找一个代码来冒泡排序一个单词列表。
mylist = [12, 5, 13, 8, 9, 65]
def bubble(badList):
length = len(badList) - 1
unsorted = True
while unsorted:
for element in range(0,length):
unsorted = False
if badList[element] > badList[element + 1]:
hold = badList[element + 1]
badList[element + 1] = badList[element]
badList[element] = hold
print badList
else:
unsorted = True
print bubble(mylist)
这段代码是用数字做的,我希望用数字来做。 谢谢
答案 0 :(得分:1)
关于Python的许多很酷的事情之一是,相等和比较运算符对字符串起作用,就像它们对数字一样。例如,如何比较两个数字是否相同?
7 == 7 # true!
两个字符串怎么样?
"Hello world".equals("Hello world") // Java...
"Hello world" == "Hello world" # Python!
现在给比较者。 Python使用词典排序来比较字符串。基本上,它会查看每个字符串中的第一个字符并说“哪个更大?”。如果它们是相同的,它继续沿着字符串向下。举几个例子:
"ABC" > "BAC" # false, because the character B is greater than A
"AAAB" < "AAAC" # true, because the character B is less than C
因此,无论mylist
是由整数还是字符串组成,您的代码都会有效。
答案 1 :(得分:1)
如果字符串冒泡排序的条件,则没有内置python的小型工作
alpha = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'j', 'K', 'L',
'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'X',
'Y','Z']
array1 = ['BACA', 'BABB', 'AB', 'AB', 'B', 'FA', 'AD', 'A']
for j in range(0, len(array1)):
for i in range(0, len(array1)):
_sorted = False
if i != len(array1)-1:
for k in range(0,len(array1[i])):
if not _sorted:
if k != (len(array1[i]) and len(array1[i+1])):
if alpha.index(array1[i][k]) > alpha.index(array1[i+1][k]):
array1[i], array1[i+1] = array1[i+1], array1[i]
_sorted = True
elif alpha.index(array1[i][k]) < alpha.index(array1[i+1][k]):
_sorted = True
else:
if len(array1[i+1]) < len(array1[i]):
array1[i], array1[i+1] = array1[i+1], array1[i]
_sorted = True
print(array1)
每次迭代的输出:
['BABB', 'AB', 'AB', 'B', 'BACA', 'AD', 'A', 'FA']
['AB', 'AB', 'B', 'BABB', 'AD', 'A', 'BACA', 'FA']
['AB', 'AB', 'B', 'AD', 'A', 'BABB', 'BACA', 'FA']
['AB', 'AB', 'AD', 'A', 'B', 'BABB', 'BACA', 'FA']
['AB', 'AB', 'A', 'AD', 'B', 'BABB', 'BACA', 'FA']
['AB', 'A', 'AB', 'AD', 'B', 'BABB', 'BACA', 'FA']
['A', 'AB', 'AB', 'AD', 'B', 'BABB', 'BACA', 'FA']
['A', 'AB', 'AB', 'AD', 'B', 'BABB', 'BACA', 'FA']
答案 2 :(得分:0)
您的冒泡排序代码中存在错误,这意味着它不会正确排序某些(可能是大多数)列表。这与列表中值的数据类型没有任何关系(它与数字列表或字符串列表有相同的问题)。
这是固定代码:
def bubble(badList):
length = len(badList) - 1
unsorted = True
while unsorted:
unsorted = False # this was moved out of the for loop
for element in range(0,length):
if badList[element] > badList[element + 1]:
hold = badList[element + 1]
badList[element + 1] = badList[element]
badList[element] = hold
print badList # comment this out when you're done testing
unsorted = True # this was moved up from the else block
它适用于数字和字符串,如下所示:
lst = [12, 5, 13, 8, 9, 65]
>>> bubble(lst)
[5, 12, 13, 8, 9, 65]
[5, 12, 8, 13, 9, 65]
[5, 12, 8, 9, 13, 65]
[5, 8, 12, 9, 13, 65]
[5, 8, 9, 12, 13, 65]
>>> lst
[5, 8, 9, 12, 13, 65]
>>> lst = ['a', 'list', 'of', 'words', 'foo', 'bar', 'baz']
>>> bubble(lst)
['a', 'list', 'of', 'foo', 'words', 'bar', 'baz']
['a', 'list', 'of', 'foo', 'bar', 'words', 'baz']
['a', 'list', 'of', 'foo', 'bar', 'baz', 'words']
['a', 'list', 'foo', 'of', 'bar', 'baz', 'words']
['a', 'list', 'foo', 'bar', 'of', 'baz', 'words']
['a', 'list', 'foo', 'bar', 'baz', 'of', 'words']
['a', 'foo', 'list', 'bar', 'baz', 'of', 'words']
['a', 'foo', 'bar', 'list', 'baz', 'of', 'words']
['a', 'foo', 'bar', 'baz', 'list', 'of', 'words']
['a', 'bar', 'foo', 'baz', 'list', 'of', 'words']
['a', 'bar', 'baz', 'foo', 'list', 'of', 'words']
>>> lst
['a', 'bar', 'baz', 'foo', 'list', 'of', 'words']