为了账号安全,请及时绑定邮箱和手机立即绑定

不反转数字的字符反转

不反转数字的字符反转

jeck猫 2021-06-21 17:54:00
我需要一个代码来只颠倒字母顺序,而不颠倒数字。对于前。假设我有:3 DF 4 B 5 H 6 K(在字符串或数组中)我应该产生输出:3 KH 4 B 5 F 6 D
查看完整描述

1 回答

?
眼眸繁星

TA贡献1873条经验 获得超9个赞

试试这个


def reverse_besides_number(l):

  # convert number in list to a dict

  # record the index

  d = {k: v for k, v in enumerate(l) if v.isdigit()}

  # record the character in list

  c = [i for i in l if not i.isdigit()]

  c.reverse()

  # insert

  for key, value in d.items():

      c.insert(key, value)

  return c


查看完整回答
反对 回复 2021-06-30
  • 1 回答
  • 0 关注
  • 187 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信