英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论