字符串篇(python)—按照给定的字母序列对单词数组排序_接收给定的单词对单词重新排序(按照单词中的字母升序排序 输出新单词)

这篇博客介绍了一种使用插入排序算法,根据给定字母序列对Python中的单词数组进行排序的方法。通过示例代码展示了如何实现,并强调了学习资源在编程学习中的重要性,提供了包括学习路线、视频教程、项目实践、电子书等丰富的学习资源。
摘要由CSDN通过智能技术生成
len2 = len(str2)
i = 0
j = 0
while i < len1 and j < len2:
    if list(str1)[i] not in char_to_int.keys():
        char_to_int[list(str1)[i]] = -1

    if list(str2)[i] not in char_to_int.keys():
        char_to_int[list(str2)[j]] = -1
    # 比较各个字符的大小
    if char_to_int[list(str1)[i]] < char_to_int[list(str2)[j]]:
        return - 1

    elif char_to_int[list(str1)[i]] > char_to_int[list(str2)[j]]:
        return 1
    else:
        i += 1
        j += 1
if i == len1 and j == len2:
    return 0
elif i == len1:
    return -1
else:
    return 1

def insertSort(s, char_to_int):
lens = len(s)
i = 1
while i < lens:
temp = s[i]
j = i - 1
while j >= 0:
if compare(temp, s[j], char_to_int) == -1:
s[j + 1] = s[j]
else:
break
j -= 1
s[j + 1] = temp
i += 1

<
  • 23
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值