Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中仅通过insert将字符串转换为另一个字符串_Python_String - Fatal编程技术网

在python中仅通过insert将字符串转换为另一个字符串

在python中仅通过insert将字符串转换为另一个字符串,python,string,Python,String,在python中,我有一些字符串,如:“我每天坐公交车去上学”和“我每天坐公交车去上学”和“你每天坐公交车回家”。我想知道,只有插入一些字符才能将第一个字符转换为其他字符吗?如果是,则获取字符及其必须插入的位置!我使用了difflib.SequenceMatcher,但是在一些有重复单词的字符串中,它不起作用 可以将第一个转换为第二个,但不能将第三个。例如: words = sentence.split() words.insert("school", 3) words.insert("bus"

在python中,我有一些字符串,如:
“我每天坐公交车去上学”
“我每天坐公交车去上学”
“你每天坐公交车回家”
。我想知道,只有插入一些字符才能将第一个字符转换为其他字符吗?如果是,则获取字符及其必须插入的位置!我使用了
difflib.SequenceMatcher
,但是在一些有重复单词的字符串中,它不起作用

可以将第一个转换为第二个,但不能将第三个。例如:

words = sentence.split()
words.insert("school", 3)
words.insert("bus", 6)
sentence = ' '.join(words)

仅通过插入无法将第二个字符串转换为任何其他字符串,因为“I”需要替换为“you”。

有趣的问题。您可以使用正则表达式来检查这一点。诸如此类:

main = "I go to by everyday"
to_search = "I go to school by bus everyday"

import re
pattern = [chr for chr in main]
pattern = "(.*?)" + "(.*?)".join(pattern) + "(.*?)"
pattern = pattern.replace(' ', '\s{1}')
pattern = re.compile(pattern)

pattern.findall(to_search)
但它可能并不完美(不能处理像
\
那样必须转义的字符,但我相信您可以调整它)

请注意,您仅通过插入来表示。因此,如果使用字符串
我每天都会去
并在字符串中的字符之间添加任意数量的字符,我将得到我想要的。因此,需要将该字符串转换为以下正则表达式:

(.*?)I(.*?)\s{1}(.*?)g(.*?)o(.*?)\s{1}(.*?)t(.*?)o(.*?)\s{1}(.*?)b(.*?)y(.*?)
请注意,空格已被
\s{1}
运算符替换,
(.*)
基本上表示“捕获所有内容并将其放入一个组”。对于第一个字符串,应得到以下结果:

[('', '', '', '', '', '', '', '', 'school ', '', '', 'bus ', '', '', '', '', '', '', '', '')]
这将告诉您必须在原始字符串的每个字符(加上最后一个字符)之前插入哪些字符串


对于第二个字符串,您将收到一个空列表,因为初始字符串不能转换为它(缺少“I”字符)。

让我们重申这个问题,并说我们正在检查s1(例如,“我每天都去”)是否可以通过插入变为s2(例如,“我每天都坐公共汽车上学”)。如果我们将字符串视为有序集,这个问题非常简单。本质上,我们是在问s1是否是s2的子集

要解决这个问题,贪婪算法就足够了(而且是最快的)。我们迭代s1中的每个字符,并尝试在s2中找到该字符的第一个匹配项。同时,我们保留一个缓冲区来保存在查找字符时遇到的所有不匹配字符,以及我们开始填充缓冲区的位置。当我们找到我们正在寻找的字符时,我们将缓冲区的位置和内容转储到一个占位符中

当我们在s2之前到达s1的末尾时,这实际上意味着s1是s2的子集,我们返回占位符。否则,s1不是s2的子集,不可能仅通过插入从s1形成s2,因此我们返回false。这个贪婪算法需要O(len(s1)+len(s2)),下面是它的代码:

#我们正在检查是否可以通过插入从s1生成s2
def检查(s1、s2):
#用于迭代s1和s2的索引
i1=0
i2=0
#用于跟踪在何处插入内容的词典
inserts=dict()
buffer=“”
pos=0
当i1

您可以并行地遍历两个字符串,为每个字符串维护索引。对于每个相等的字符,增加两个索引-对于每个不相等的字符,只需将索引增加到要测试的字符串中:

def f(s, t):
  """Yields True if 's' can be transformed into 't' just by inserting characters,
  otherwise false
  """

  lenS = len(s)
  lenT = len(t)

  # 's' cannot possible get turned into 't' by just insertions if 't' is shorter.
  if lenS > lenT:
    return False

  # If both strings are the same length, let's treat 's' to be convertible to 't' if
  # they are equal (i.e. you can transform 's' to 't' using zero insertions). You
  # may want to return 'False' here.
  if lenS == lenT:
    return s == t

  idxS = 0
  for ch in t:
    if idxS == lenS:
      return True
    if s[idxS] == ch:
      idxS += 1

  return idxS == lenS
这让你很生气

f('', 'I go to by everyday')                               # True
f('I go to by everyday', '')                               # False
f('I go to by everyday', 'I go to school by bus everyday') # True
f('I go to by everyday', 'you go to home by bus everyday') # False

请给出一个示例,其中
difflib.SequenceMatcher
不适用于您。如果函数得到
I God by Daily
“you go to to to bus Daily
,则该函数是否应为true?毕竟,仅通过插入字符(开头的
I
避免了这一点)无法从前者获得后者。相关:支持模糊匹配:
regex.match('(?:%s){I}'%''.join(map(regex.escape,first_one)),other)
。注意:它不回答插入的位置和字符。
f('', 'I go to by everyday')                               # True
f('I go to by everyday', '')                               # False
f('I go to by everyday', 'I go to school by bus everyday') # True
f('I go to by everyday', 'you go to home by bus everyday') # False