如何在python中不使用内置方法查找关键字前后的单词

如何在python中不使用内置方法查找关键字前后的单词,python,Python,我想使用内置方法查找关键字wihtout前后的单词 代码使用分区方法工作正常 mystring = "This is python class" keyword = ' is' before_keyword, keyword, after_keyword = mystring.partition(keyword) l = [] l.append(before_keyword) l.append(keyword) l.append(after_keyword) print(l

我想使用内置方法查找关键字wihtout前后的单词

代码使用
分区
方法工作正常

mystring =  "This is python class"
keyword = ' is'
before_keyword, keyword, after_keyword = mystring.partition(keyword)
l = []
l.append(before_keyword)
l.append(keyword)
l.append(after_keyword)
print(l)
我的输出和预期输出如下

['This','is','python class']

用于测试的样本输入 输入>>
“这是要测试的hello”

关键字>>
'is'


您可以尝试使用
插入
拆分
,如下所示:

mystring = mystring.split(keyword)
mystring.insert(1, keyword)
print(mystring)
mystring =  "This is python class"
keyword = ' as'
l = []
x = mystring.find(keyword)
if x != -1:
    l.append(mystring[:x])
    l.append(keyword)
    l.append(mystring[x+len(keyword):])
    print (l)
else:
    print (keyword, 'is not found in', mystring)
输出:

['This', ' is', ' python class']
['This', 'is', 'python class and it', 'is', 'a unicorn']
['This', ' is', ' python class and it', ' is', ' a unicorn']
['This is python class and it is a', ' unicorn']
 as is not found in This is python class and it is a unicorn
['This', ' is python class and it is a unicorn']

编辑:

要使代码适用于各种字符串,可以使用以下大量代码:

mystring =  "This is python class and it is a unicorn"
keyword = 'is'
newlist = []
if keyword in mystring:
    string = ''
    for i in mystring.split():
        if i == keyword:
            newlist.append(string)
            newlist.append(i)
            string = ''
        else:
            string += i + ' '
    newlist.append(string)
    newlist = [i.strip() for i in newlist if i]
print(newlist)
输出:

['This', ' is', ' python class']
['This', 'is', 'python class and it', 'is', 'a unicorn']
['This', ' is', ' python class and it', ' is', ' a unicorn']
['This is python class and it is a', ' unicorn']
 as is not found in This is python class and it is a unicorn
['This', ' is python class and it is a unicorn']

假设您不想使用内置方法,您可以创建自己的
分区
非分区
:p

inp = "This hello is to test"

def not_partition(inp='', part_at=''):
    inp = inp.strip().split()
    ret = ['']
    for i,v in enumerate(inp):
        if v==part_at:
            ret = ret+[part_at, '']
        else:
            ret[-1] = (ret[-1]+' '+v).strip()
    if ret[-1]=='': del ret[-1]
    return ret

not_partition(inp, 'is'), not_partition(inp, 'test')
您还可以创建一个更高效的递归
not\u分区
:D

def not_partition(inp='', part_at=''):
    inp = inp.strip().split()
    i=0
    while i<len(inp):
        if inp[i]==part_at:
            return [' '.join(inp[:i]), part_at]+not_partition(' '.join(inp[i+1:]),part_at)
        else:
            i+=1
    return [' '.join(inp)] if inp!=[] else []
not_partition(inp, 'is'), not_partition(inp, 'test'), not_partition(inp, 'what?')
没有任何功能。只需使用
==
如果不允许使用任何内置函数,那么其中一个选项就是进行字符串比较。我假设您也不能使用len()内置函数

mystring =  "This is python class and it is a unicorn"
keyword = 'is'
list_of_words = []
word = ''
key_len = str_len = 0
#to get length of keyword as you cannot use len(keyword)
for _ in keyword: key_len +=1
for _ in mystring: str_len +=1
i = 0
start_pos = 0
if keyword in mystring:
    while i < str_len:
        if mystring[i:i+key_len] == keyword:
            if i != 0:
                list_of_words.append(mystring[start_pos:i])
            list_of_words.append(keyword)
            i += key_len
            start_pos = i
        else:
            i+=1
    if start_pos != str_len and start_pos != 0:
        list_of_words.append(mystring[start_pos:])
if list_of_words:
    print (list_of_words)
else:
    print (keyword, 'is not found in', mystring)
输出:

['This', ' is', ' python class']
['This', 'is', 'python class and it', 'is', 'a unicorn']
['This', ' is', ' python class and it', ' is', ' a unicorn']
['This is python class and it is a', ' unicorn']
 as is not found in This is python class and it is a unicorn
['This', ' is python class and it is a unicorn']
输入字:

mystring =  "This is python class and it is a unicorn"
keyword = ' is'
mystring =  "This is python class and it is a unicorn"
keyword = ' unicorn'
mystring =  "This is python class and it is a unicorn"
keyword = ' as'
mystring =  "This is python class and it is a unicorn"
keyword = 'This'
输出:

['This', ' is', ' python class']
['This', 'is', 'python class and it', 'is', 'a unicorn']
['This', ' is', ' python class and it', ' is', ' a unicorn']
['This is python class and it is a', ' unicorn']
 as is not found in This is python class and it is a unicorn
['This', ' is python class and it is a unicorn']
输入字:

mystring =  "This is python class and it is a unicorn"
keyword = ' is'
mystring =  "This is python class and it is a unicorn"
keyword = ' unicorn'
mystring =  "This is python class and it is a unicorn"
keyword = ' as'
mystring =  "This is python class and it is a unicorn"
keyword = 'This'
输出:

['This', ' is', ' python class']
['This', 'is', 'python class and it', 'is', 'a unicorn']
['This', ' is', ' python class and it', ' is', ' a unicorn']
['This is python class and it is a', ' unicorn']
 as is not found in This is python class and it is a unicorn
['This', ' is python class and it is a unicorn']
输入字:

mystring =  "This is python class and it is a unicorn"
keyword = ' is'
mystring =  "This is python class and it is a unicorn"
keyword = ' unicorn'
mystring =  "This is python class and it is a unicorn"
keyword = ' as'
mystring =  "This is python class and it is a unicorn"
keyword = 'This'
输出:

['This', ' is', ' python class']
['This', 'is', 'python class and it', 'is', 'a unicorn']
['This', ' is', ' python class and it', ' is', ' a unicorn']
['This is python class and it is a', ' unicorn']
 as is not found in This is python class and it is a unicorn
['This', ' is python class and it is a unicorn']
使用查找功能 您可以按如下方式搜索关键字并创建列表:

mystring = mystring.split(keyword)
mystring.insert(1, keyword)
print(mystring)
mystring =  "This is python class"
keyword = ' as'
l = []
x = mystring.find(keyword)
if x != -1:
    l.append(mystring[:x])
    l.append(keyword)
    l.append(mystring[x+len(keyword):])
    print (l)
else:
    print (keyword, 'is not found in', mystring)

您也可以尝试使用正则表达式解决此问题

import re

keyword = 'is'
str1 = "This hello is to test"
partition = re.split(r'\s({0})\s'.format(keyword), str1)
partition

['This hello', 'is', 'to test']

找到单词在列表中的位置。然后将索引前的所有值分配给before_关键字,将搜索词的index+len后的所有值分配给after_关键字您是说不能使用
find
作为选项吗?如果找不到关键字,它将提供不正确的结果。此外,关键字可能并不总是IST中的第二个元素,这是一个庞大的代码:P@KuldeepSinghSidhu哈哈,是的:-)为什么第10行
string=''
是doing@Nons我一直在向
字符串
变量添加值,当它看到一个单词与
关键字
变量相同时(在本例中,
is
),它将把
string
变量作为一个元素添加到列表中,然后添加关键字(
is
),然后将
string
变量重置为空字符串。提问者要求的解决方案不需要任何python的“内置方法”(如果我理解正确的话).
strip
split
等您在解决方案中使用的方法是“内置方法”…是的。没有内置函数可以使用。因此我们的解决方案必须没有任何这些选项。是的!如果你想让我也写这些,请告诉我!
not_strip
not_split
:P但是,你也必须做
not_len
可能
not_append
~),还必须使用
拆分字符串:
。。。它将成为一个全新的世界!如果OP要求,我将更新!我们都上当了。我们不能使用内置函数。这就像是治疗。编写代码时不使用任何内置函数:)“这是python类,它是独角兽”不起作用。多次检查关键字。嗯,我没想过。抢手货我一找到关键字就要跳出循环。修复了一个关键字多次出现的问题。还检查单词是字符串中的第一个还是最后一个单词。如果单词是第一个,则忽略上一个。若单词是最后一个,那个么after将被忽略。如果要打印空字符串以表示开头或后面,请告诉我。请注意,像
as
is
这样的单词将被拆分为
。如果要消除这种情况,代码可能会变得更复杂。我知道这是一个python类练习,因此与您一起研究解决方案。在现实世界中,您将使用内置的方法/函数来实现这一点。