0

我是 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)

这段代码是用数字来做的,我想要一个用文字来做的。谢谢

4

3 回答 3

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是由整数还是字符串组成,您的代码都可以正常工作。

于 2013-10-01T00:32:11.077 回答
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']
于 2020-01-30T20:38:23.677 回答
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']
于 2013-10-01T00:53:07.167 回答