0
0

More than 3 years have passed since last update.

【python3】いずれかの文字列が含まれるかの判定

Posted at

またまたメモ書きの駄ブログ。

概要

以下のプログラムで期待する処理が動いたことは確認できた。

if 'soccer' in hogehoge or 'baseball' in hogehoge:

しかし数が多いとプログラムが横に長くなるので、
まとめて判定できないかとふと思った。

テストその1

if {'soccer', 'baseball'} in hogehoge:

これはTypeError。
なんか色々と間違ってる。

テストその2

import re

try:
    re.search('(soccer|baseball)', hogehoge).group(1)
except AttributeError:
    pass

これは出来そうだけど例外処理が入るので1行で書けない。

テストその3

import re

if re.search('(soccer|baseball)', hogehoge) is not None:

これはいいかも!
けどまだ短くできるのでは・・・?


その他いいものがあれば募集します!

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0