首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在一个块中检查两个条件,而不是在python中检查两个

如何在一个块中检查两个条件,而不是在python中检查两个
EN

Stack Overflow用户
提问于 2021-02-10 19:23:36
回答 2查看 41关注 0票数 2
代码语言:javascript
复制
            if 'hiring ' in job_title:
                job_title = job_title.split("hiring ")[1]
                if job_title.startswith('– '):
                    job_title = job_title.replace('– ', '')
            elif 'Hiring ' in job_title:
                job_title = job_title.split("Hiring ")[1]
                if job_title.startswith('– '):
                    job_title = job_title.replace('– ', '')
            job_titles.append(job_title)

所以我有这段代码,我想检查字符串变量job_title中是否有‘雇用’或‘雇用’,然后通过拆分job_title...I来获得这个单词后面的文本。我想在一个单独的statement...but中这样做,如果现在我有two...How,我可以这样做吗,因为对于一个语句:

代码语言:javascript
复制
            if 'hiring ' or 'Hiring ' in job_title:
                job_title = job_title.split("hiring ")[1]
                if job_title.startswith('– '):
                    job_title = job_title.replace('– ', '')

我不得不在“雇佣”和“雇佣”这两种情况下分道扬清...有什么想法吗?

EN

回答 2

Stack Overflow用户

发布于 2021-02-10 22:29:23

这里是一种使用正则表达式在字符串的一部分上执行不区分大小写的替换的方法,直到并包括第一个“雇佣”实例,可选地跟随一系列空白,可选地跟随单个短划线,并且可选地跟随进一步的空白序列。

这就是模式:r'(?i)^.*?hiring\s*-?\s*'

这就是它的实际作用:

代码语言:javascript
复制
import re

word = 'hiring'
pattern = rf'(?i)^.*?{word}\s*-?\s*'

test_strings = ['Example Co. is Hiring example staff for example.',
                'Example Co. is hiring example staff for example.',
                'Example Co. is HiRiNg example staff for example.',
                'Example Co. is Hiring - example staff for example.',
                'Example Co. is Hiring     -   example staff for example.',
                'Example Co. is example staff for example.',
                'Example Co. is Hiring example staff for hiring example.',
               ]

for s in test_strings:
    print(re.sub(pattern, '', s))

输出

代码语言:javascript
复制
example staff for example.
example staff for example.
example staff for example.
example staff for example.
example staff for example.
Example Co. is example staff for example.
example staff for hiring example.

与您的代码一致,最后一个测试字符串显示,替换只适用于单词“雇用”的第一个实例。如果该单词出现在字符串的后面,则不会考虑该单词。如果您希望将替换应用到最后一个实例,您可以删除目标单词前面的非贪婪修饰语,即

代码语言:javascript
复制
>>> pattern = rf'(?i)^.*{word}\s*-?\s*'
>>> print(re.sub(pattern, '', test_strings[-1]))
example.
票数 1
EN

Stack Overflow用户

发布于 2021-02-10 20:28:08

您可以使用以下条件来实现您的目的。它利用了Walrus operator

有关行为,请参阅示例。如果不需要该空间,请使用strip()。

代码语言:javascript
复制
>>> def get_text(text):
...     if (len(info:=text.lower().split('hiring'))>1):
...             return info[-1]
...     else:
...             return 'no hiring found'
... 


```>>> get_text('Example Co. is hiring example staff for example')

‘以员工为例’

get_text('Example公司正在为example')

招聘模范员工

‘以员工为例’

get_text('Example公司是example')

的范例员工

‘找不到招聘人员’

get_text('HIRING是example')

的示例员工

‘以员工为例’

get_text('example')

‘找不到招聘人员’

get_text('')

‘找不到招聘人员’

代码语言:javascript
复制
Provided you want to keep the case you can modify the function to make a lower case copy and check in that.  See below.

```javascript

def second_method(text):

..。textcopy = text.lower()

..。如果在textcopy中使用‘雇佣’:

..。return texttextcopy.index(‘雇用’)+len(‘雇用’):

..。

YGGUN jb'),

second_method('something,HIRing

‘某事YGGUN jb’

代码语言:javascript
复制
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66135910

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档