如何從 Python 元組中 grep 特定關鍵字?
如果你有一個字串元組並想搜尋特定的字串,你可以使用 in 運算子 。
示例
tpl = ("Hello", "world", "Foo", "bar")
print("world" in tpl)輸出
這將輸出 -
True
示例
如果你想檢查是否存在子字串。你可以迴圈遍歷元組並使用以下方法查詢
tpl = ("Hello", "world", "Foo", "bar")
for i in tpl:
if "orld" in i:
print("Found orld in " + i )輸出
這將輸出 -
Found orld in world
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP