簡體   English   中英

python:檢查子字符串是否在字符串元組中

[英]python: check if substring is in tuple of strings

檢查字符串元組中是否存在多個子字符串的最優雅方法是哪種?

tuple = ('first-second', 'second-third', 'third-first') 
substr1 = 'first' 
substr2 = 'second' 
substr3 = 'third'
#if substr1 in tuple and substr2 in tuple and substr3 in tuple:
#    should return True
any(substr in str_ for str_ in tuple_)

您可以從此開始,也可以查看all()

您需要遍歷每個子字符串的元組,因此使用anyall

all(any(substr in s for s in data) for substr in ['first', 'second', 'third'])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM