将我的列表中的每个单词按升序排列。

huangapple go评论78阅读模式
英文:

How to arrange each word in my list in ascending order?

问题

我无法使用交换算法将列表中每个单词的字母按升序排列。它不起作用,因为字母没有按升序排列。

我使用的代码如下:

new_word_list = ['rudndr', 'jdkdjj', 'jdueir', 'llikdd', 'ldjdoe', 'peoods', 'hfjdgg']

ascending_list = []

for i in range(len(new_word_list)):
    new_word_list[i] = list(new_word_list[i])

for i in range(len(new_word_list)):
    for j in range(len(new_word_list[i])):
        if new_word_list[i][j-1] > new_word_list[i][j]:
            new_word_list[i][j-1], new_word_list[i][j] = new_word_list[i][j], new_word_list[i][j-1]
    ascending_list.append(new_word_list[i])

我得到的结果是:

[['d', 'n', 'd', 'r', 'r', 'u'],
 ['j', 'd', 'j', 'j', 'd', 'k'],
 ['e', 'i', 'j', 'd', 'r', 'u'],
 ['d', 'd', 'i', 'k', 'l', 'l'],
 ['d', 'd', 'e', 'j', 'l', 'o'],
 ['d', 'e', 'o', 'o', 'p', 's'],
 ['d', 'f', 'g', 'g', 'h', 'j']]
英文:

I am not able to arrange the letters of each word of a list in
ascending order using the swap algorithm. It's not working as the
letters aren't arranged in ascending fashion.

The code that I have used is as follows:

new_word_list = ['rudndr', 'jdkdjj', 'jdueir', 'llikdd', 'ldjdoe', 'peoods', 'hfjdgg']

ascending_list = []

for i in range(len(new_word_list)):
    new_word_list[i] = list(new_word_list[i])

for i in range(len(new_word_list)):
    for j in range(len(new_word_list[i])):
        if new_word_list[i][j-1] > new_word_list[i][j]:
            new_word_list[i][j-1], new_word_list[i][j] = new_word_list[i][j], new_word_list[i][j-1]
    ascending_list.append(new_word_list[i])

the result I am getting is:

> [['d', 'n', 'd', 'r', 'r', 'u'], ['j', 'd', 'j', 'j', 'd', 'k'], ['r',
> 'e', 'i', 'j', 'd', 'u'], ['i', 'k', 'd', 'd', 'l', 'l'], ['j', 'd',
> 'l', 'e', 'd', 'o'], ['o', 'o', 'd', 'p', 'e', 's'], ['h', 'd', 'g',
> 'g', 'f', 'j']]

答案1

得分: 1

new_word_list = ['rudnd', 'jdkdjj', 'jdueir', 'llikdd', 'ldjdoe', 'peoods', 'hfjdgg']

ascending_list = []

for i in range(len(new_word_list)):
    new_word_list[i] = list(new_word_list[i])

for i in range(len(new_word_list)):
    for j in range(len(new_word_list[i])):
        for k in range(0, len(new_word_list[i]) - j - 1):
            if new_word_list[i][k] > new_word_list[i][k + 1]:
                new_word_list[i][k], new_word_list[i][k + 1] = new_word_list[i][k + 1], new_word_list[i][k]

    ascending_list.append(new_word_list[i])

print(ascending_list)
英文:

https://i.imgur.com/FcOk0CZ.png

new_word_list = ['rudndr', 'jdkdjj', 'jdueir', 'llikdd', 'ldjdoe', 'peoods', 'hfjdgg']

ascending_list = []

for i in range(len(new_word_list)):
    new_word_list[i] = list(new_word_list[i])

for i in range(len(new_word_list)):
    for j in range(len(new_word_list[i])):
        for k in range(0, len(new_word_list[i]) - j - 1):
            if new_word_list[i][k] > new_word_list[i][k + 1]:
                new_word_list[i][k], new_word_list[i][k + 1] = new_word_list[i][k + 1], new_word_list[i][k]

    ascending_list.append(new_word_list[i])

print(ascending_list)

huangapple
  • 本文由 发表于 2023年8月4日 22:34:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76839091.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定