【发布时间】:2014-11-14 12:48:16
【问题描述】:
刚从 python 开始,如果我听起来很笨,请原谅。
假设以下输入:
我的文件内容:
我们喜欢独角兽
我们喜欢啤酒
我们喜欢免费(一种免费的啤酒)
我预计以下内容会返回 true:
# my_file = some path to valid file
with open(my_file) as f:
lines = f.readlines()
if 'beer' in lines:
print("found beer") # this does not happen
还是我太习惯了c#的方式,之后我会得到所有匹配的行:
// assuming I've done a similar lines = open and read from file
var v = from line in lines
where line.Contains("beer")
select line;
例如,获取那些包含beer 的行的pythonian 等价物是什么?
【问题讨论】: