如何从 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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 程序设计
C++
C#
MongoDB
MySQL
Javascript
PHP