Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Text-python中查找字符串和行号_Python_String - Fatal编程技术网

在Text-python中查找字符串和行号

在Text-python中查找字符串和行号,python,string,Python,String,我想在大文本中搜索字符串并检索其行号。 有没有一种方法在python中不包含2个for循环。这将为您提供索引 In [112]: lines = filehandle.readlines() In [113]: for elem in lines: .....: if elem.find(substr) > -1: .....: print lines.index(elem) .....: 包括substr多次发生的所有索引 In [122]:

我想在大文本中搜索字符串并检索其行号。
有没有一种方法在python中不包含2个for循环。

这将为您提供索引

In [112]: lines = filehandle.readlines()

In [113]: for elem in lines:
   .....:     if elem.find(substr) > -1:
   .....:         print lines.index(elem)
   .....:
包括substr多次发生的所有索引

In [122]: text = ['abc', 'def', 'ghi']

In [123]: for elem in text:
   .....:     if elem.find('e') > -1:
   .....:         print text.index(elem)
   .....:
1

这将为您提供索引

In [112]: lines = filehandle.readlines()

In [113]: for elem in lines:
   .....:     if elem.find(substr) > -1:
   .....:         print lines.index(elem)
   .....:
包括substr多次发生的所有索引

In [122]: text = ['abc', 'def', 'ghi']

In [123]: for elem in text:
   .....:     if elem.find('e') > -1:
   .....:         print text.index(elem)
   .....:
1

您可以使用
filter
将其过滤掉。提供一个lambda函数,该函数适用于您想要的条件(例如,这里是线的匹配)

作为第二个参数,给出要检查的所有行的列表(迭代器)。 请注意,我使用
izip
,为lambda函数提供
(行,行号)
元组的迭代器

请查找以下功能: 如您所见,这里的限制是,这只适用于小于
2^31-1
行的文件

另外,请注意,它返回一个行号列表,其中包含所有匹配行

from itertools import izip
def find_line_num_in_file(file, line):
    f = open(file, "r")
    matches = filter(lambda x: line in x[0], izip(f.readlines(), xrange(-1 + 2**31)))
    f.close()
    return [m[1] for m in matches]
如果您碰巧已经拥有了这些行(即,不是迭代器),您可以这样做

def find_line_num_in_lines(lines, line):
    matches = filter(lambda x: line in x[0], zip(lines, range(len(lines))))
    return [m[1] for m in matches]

您可以使用
filter
将其过滤掉。提供一个lambda函数,该函数适用于您想要的条件(例如,这里是线的匹配)

作为第二个参数,给出要检查的所有行的列表(迭代器)。 请注意,我使用
izip
,为lambda函数提供
(行,行号)
元组的迭代器

请查找以下功能: 如您所见,这里的限制是,这只适用于小于
2^31-1
行的文件

另外,请注意,它返回一个行号列表,其中包含所有匹配行

from itertools import izip
def find_line_num_in_file(file, line):
    f = open(file, "r")
    matches = filter(lambda x: line in x[0], izip(f.readlines(), xrange(-1 + 2**31)))
    f.close()
    return [m[1] for m in matches]
如果您碰巧已经拥有了这些行(即,不是迭代器),您可以这样做

def find_line_num_in_lines(lines, line):
    matches = filter(lambda x: line in x[0], zip(lines, range(len(lines))))
    return [m[1] for m in matches]

即使我们提供一个
文件句柄
并用变量或其他东西替换
“strearch”
,此代码仍然无法工作。我们在这里抓什么?没有抛出异常。而且,即使它有效,它也只会匹配整行,而不会匹配其中的子字符串-1即使我们提供一个
文件句柄
并用变量或其他东西替换
“strSearch”
,此代码仍然无法工作。我们在这里抓什么?没有抛出异常。而且,即使它有效,它也只会匹配整行,而不会匹配其中的子字符串-1看一看python教程,或者看一看enumerate()。看一看python教程,或者看一看enumerate()。@eumiro:是的,但它是包含子字符串的元素,包含substr的元素的索引为required@eumiro:我是在检查元素中是否存在substr的
if
条件下执行此操作的。这将不起作用,因为第二个
readlines()
将返回
[]
。在第一个
readlines()
之后,当前位置将移动到文件的末尾。@eumiro:是的,但它是包含子字符串的元素,包含substr的元素的索引为required@eumiro:我是在检查元素中是否存在substr的
if
条件下执行此操作的。这将不起作用,因为第二个
readlines()
将返回
[]
。在第一个
readlines()
之后,当前位置将移动到文件的末尾。