Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.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 基于空格查找字符串中的字符_Python_Python 3.x - Fatal编程技术网

Python 基于空格查找字符串中的字符

Python 基于空格查找字符串中的字符,python,python-3.x,Python,Python 3.x,所以我试图让一个函数工作,它将返回一个新的单个字符列表,这些字符紧跟在另外两个给定字符之后。像这样: def filter_possible_chars(corpus, last): """ >>> filter_possible_chars('lazy languid line', 'la') ['z', 'n'] >>> filter_possible_chars('pitter patter', 'tt') ['e

所以我试图让一个函数工作,它将返回一个新的单个字符列表,这些字符紧跟在另外两个给定字符之后。像这样:

def filter_possible_chars(corpus, last):
    """
    >>> filter_possible_chars('lazy languid line', 'la')
    ['z', 'n']
    >>> filter_possible_chars('pitter patter', 'tt')
    ['e', 'e']
    """
    char_list = []
    corpus_split = corpus.split()
    for word in corpus_split:
        if last in word:
            word_split = word.split(last)
            follows_last = word_split[1]
            char_list.append(follows_last[0])
    return char_list
此函数适用于docstring中给出的示例,但我需要包含包含空白的示例,例如:

>>> filter_possible_chars('when the goat jumped to the rock', ' t')
它将返回:

['h', 'o', 'h']
但由于我的函数显然是删除空白,我想我需要尝试一种完全不同的方法。我考虑过不要将字符串拆分为单个单词,并尝试用给定的字母索引到其中,但我想不出一种方法可以使它在一个字符串中的多个实例中工作

>>> pat="tt"
>>> corpus="pitter patter"
>>> print(re.findall("%s(.)"%pat,corpus))
['e', 'e']
>>> corpus,pat = 'when the goat jumped to the rock', ' t'
>>> re.findall("%s(.)"%pat,corpus)
['h', 'o', 'h']
>>> corpus,pat = 'lazy languid line', 'la'
>>> re.findall("%s(.)"%pat,corpus)
['z', 'n']
解释
  • %
    是运算符,因此,例如
    %s(.)“%%la”
    的计算结果为
    “la(.)”

  • 在中,
    是“任意字符”的模式,
    ()
    定义可在以后检索其值的组,例如使用:

    如果模式中存在一个或多个组,则返回组列表


因此,例如,模式
la(.)
意味着“搜索
la
,后跟任何字符,并捕获该字符”。

解决此问题的方法非常好。不要将句子拆分成单词,你应该尝试在完整的
语料库中查找
last
的所有实例。但是,嘿,实际上,
split
函数可以为您实现这一点

corpus=“当山羊跳到岩石上时”
spl=语料库分割('t')
打印声压级
>>[“当”,“他山羊跳”,“哦”,“他摇滚”]
res=[x[0]表示spl[1]中的x,如果len(x)>0]
打印资源
>>['h','o','h']

因此,您可以按
last
拆分
语料库
,然后从拆分结果中获取所有字符串,而不包含第一个字符串(因为它不是以
last
开头),然后从每个这样的字符串中获取第一个字母

好的,很好,太棒了。你能解释一下
re.findall(“%s()%pat,corpus)
到底在做什么吗?我熟悉
re.findall(pattern,string,flags=0)
,但不确定符号到底在做什么或它们在pattern参数中的含义。@RobertHemingway
re.findall()
搜索字符串中正则表达式模式的所有非重叠匹配项<代码>'%s(.)'%pat
在说“给我该正则表达式模式后的第一个字符”。
()
表示除换行符以外的任何单个字符。我喜欢你解释了你使用的正则表达式的基本原理(所以请投票支持你的答案)。但总的来说,我认为最好不要使用正则表达式,只要你真的不需要它。。。。但我确实听到了你的话。。。但是按照这个逻辑,你永远不会使用正则表达式,因为给猫剥皮的方法总是不止一种。。。