比较来自世界各地的卖家的域名和 IT 服务价格

使用 python 搜索某些字符的文本文件

我的问题就像
https://coderoad.ru/4785244/
, 除了 , 我想找到几个的进入
chars

, 例如
g

,
d


e

, 然后打印其中的字符串 ALL 有指定的字符。

我尝试了以下几点,但它没有工作:


searchfile = open/"myFile.txt", "r"/
for line in searchfile:
if /'g' and 'd'/ in line: print line,
searchfile.close//


我收到了它的线条 EITHER 'g' 或者 'd' 或者,另一种是我所需要的, - 这只是两种情况,而不是至少一个,因为执行上述代码的结果。
已邀请:

奔跑吧少年

赞同来自:

if set/'gd'/.issubset/line/


这具有它的优势,因为每次检查
c in line

在整个行中重复

诸葛浮云

赞同来自:

这条线:


if /'g' and 'd'/ in line:


这与


if 'd' in line:


因为


>>> 'g' and 'd'
'd'


你想要


if 'g' in line and 'd' in line:


甚至更好:


if all/char in line for char in 'gde'/:


/您还可以使用集合的交点,但它的广泛性较少。/

莫问

赞同来自:

当涉及映射模式时,正则表达式肯定会帮助您,但似乎您的搜索比它更容易。 尝试执行以下操作:


# in_data, an array of all lines to be queried /i.e. reading a file/
in_data = [line1, line2, line3, line4]

# search each line, and return the lines which contain all your search terms
for line in in_data:
if /'g' in line/ and /'d' in line/ and /'e' in line/:
print/line/


这么简单的东西应该起作用。 在这里,我提出了一些假设:
1. 搜索查询的顺序无关紧要
2. 上 / 不考虑较低的登记册
3. 未考虑搜索查询的频率。

我希望它会有所帮助。

要回复问题请先登录注册