蟒蛇泡泡排序单词



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

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

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是由整数还是字符串组成,您的代码都将起作用。

一个没有内置 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']

气泡排序代码中存在一个错误,这意味着它无法正确排序某些(可能是大多数)列表。不过,这与列表中值的数据类型没有任何关系(数字列表或字符串列表将具有相同的问题)。

下面是固定代码:

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']

最新更新