【问题标题】:how to check two conditions in a single block rather than 2 in python如何检查单个块中的两个条件而不是python中的2个
【发布时间】:2021-05-14 01:59:09
【问题描述】:
            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)

所以我有这段代码的 sn-p,我想检查字符串变量 job_title 中是否包含“招聘”或“招聘”,然后通过拆分 job_title 来获取该单词之后的文本...我想在单个 if 语句中执行...但现在我有两个...我该怎么做,因为对于单个语句:

            if 'hiring ' or 'Hiring ' in job_title:
                job_title = job_title.split("hiring ")[1]
                if job_title.startswith('– '):
                    job_title = job_title.replace('– ', '')

我必须在“招聘”或“招聘”中分开......有什么想法吗?

【问题讨论】:

  • 我想你会发现使用Regular Expressions 会更适合你的任务。
  • 我对python比较陌生,发现正则表达式真的很难……还有其他方法吗
  • 它们可能有点棘手,但绝对值得学习,相信我。观看一些教程,尝试一些练习,您就会意识到它们的用处。
  • 好的,所以对于这个例子,我想要'hiring'或'Hiring'之后的字符串的RE ...例如“Example Co.正在招聘示例员工”......你可以构造RE为此
  • 我使用了这种模式:pattern = re.compile(r'(?

标签: python-3.x string if-statement replace split


【解决方案1】:

您可以将以下条件用于您的目的。它利用Walrus operator
请参阅行为示例。如果您不想要空间,请使用 strip()。

>>> 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')
' example staff for example'
>>> get_text('Example Co. is HIRING example staff for example')
' example staff for example'
>>> get_text('Example Co. is example staff for example')
'no hiring found'
>>> get_text('HIRING is example staff for example')
' is example staff for example'
>>> get_text('example')
'no hiring found'
>>> get_text('')
'no hiring found'
>>> 

如果您想保留大小写,您可以修改函数以制作小写副本并签入。见下文。

>>> def second_method(text):
...     textcopy = text.lower()
...     if 'hiring' in textcopy:
...             return text[textcopy.index('hiring') + len('hiring'):]
... 
>>> second_method('something HIRing Some Thing YGGUN jb')
' Some Thing YGGUN jb'
>>> 

【讨论】:

  • 我不希望其余文本为小写...这就是问题所在...我尝试使用 lower 同时检查“招聘”和“招聘”,但这也是将字符串的其余部分转换为我不想要的小写
  • 复制并使用它。请参阅我添加的另一个函数中的示例。
【解决方案2】:

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

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

它在行动:

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))

输出

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.

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

>>> pattern = rf'(?i)^.*{word}\s*-?\s*'
>>> print(re.sub(pattern, '', test_strings[-1]))
example.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-24
    • 2021-04-17
    • 2012-01-11
    • 2014-01-25
    • 1970-01-01
    • 1970-01-01
    • 2019-08-08
    • 2013-02-18
    相关资源
    最近更新 更多