多个字符串替换python中的位置

weixin_38060280 2019-09-12 11:24:20
我需要用扩充字符串替换字符串中的字符串. 我的问题是,每次更换信息中字符串的位置和原始字符串更改:如何以pythonic方式更新它? str = 'If you do not know where you are going, any road will get you there.' info = {'you': [(3, 3), (25, 3)], 'get you': [(54, 7)], 'know': [(14, 4)]} # replace 'you' from info with '<b>you</b>' and 'know' with '<i>know</i>' # results in str = 'If <b>you</b> do not <i>know</i> where <b>you</b> are going, any road will get you there.' info = {'<b>you</b>': [(3, 10), (25, 10)], 'get you': [(54, 7)], '<i>know</i>': [(21, 11)]} 到目前为止我的解 str = 'If you do not know where you are going, any road will get you there.' info = {'you': [(3, 3), (25, 3)], 'get you': [(54, 7)], 'know': [(14, 4)]} replacer = [('you', '<b>you</b>'), ('know', '<i>know</i>')] for s, s2 in replacer: print "replacing %s to %s and update position info dict" % (s, s2) old_s_pos = info[s] diff = len(s2) - len(s) new_key_pos = [(old_s_pos[0][0], old_s_pos[0][1] + diff)] old_s_pos = old_s_pos[1:] if old_s_pos: next_old_s_pos_start = old_s_pos[0][0] else: next_old_s_pos_start = None del info[s] for key, positions in info.iteritems(): new_positions = [] for i, (x,y) in enumerate(positions): if x < next_old_key_pos_start: new_positions.append((x + diff, y)) else: new_positions.append((x, y)) if next_old_s_pos_start is not None: # update old_s_pos at first pair new_key_pos.append((old_key_pos[0][0], old_s_pos[0][1] + diff)) old_s_pos = old_s_pos[1:] if old_s_pos: next_old_s_pos_start = old_s_pos[0][0] info[key] = new_positions info[s2] = new_key_pos print info 亲切的问候,马蒂亚斯
...全文
146 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_38061618 2019-09-12
  • 打赏
  • 举报
回复
这是基于你应该只通过字符串替换倒退的想法.首先,我必须稍微修改您的信息和替换器结构: str = 'If you do not know where you are going, any road will get you there.' info = {'you': [(3, 3), (25, 3)], 'get you': [(54, 7)], 'know': [(14, 4)]} replacer = [('you', '<b>you</b>'), ('know', '<i>know</i>')] info2 = {} replacer2 = {} for original, replacement in replacer: replacer2[original] = replacement for k, v in info.items(): for start, length in v: replacement = None if k in replacer2: replacement = replacer2[k] info2[start] = (k, length, replacement) for position in sorted(info2.iterkeys(), reverse=True): original, length, replacement = info2[position] if replacement is not None: str[:position] + replacement + str[position + length] print str

476

社区成员

发帖
与我相关
我的任务
社区描述
其他技术讨论专区
其他 技术论坛(原bbs)
社区管理员
  • 其他技术讨论专区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧