在本文中,我们将给您介绍关于在python中,如何“如果finditer的详细内容,并且为您解答...没有匹配项”?的相关问题,此外,我们还将为您提供关于python–如何使用不同的数字替换字符串中的
在本文中,我们将给您介绍关于在python中,如何“如果finditer的详细内容,并且为您解答...没有匹配项”?的相关问题,此外,我们还将为您提供关于python – 如何使用不同的数字替换字符串中的所有匹配项、Python-如何查找列表中某个元素的所有匹配项?、zip(* [iter(s)] * n)在Python中如何工作?、为什么在Python中将“ dir()”命名为“ dir”?的知识。
本文目录一览:- 在python中,如何“如果finditer(...)没有匹配项”?(python finditer)
- python – 如何使用不同的数字替换字符串中的所有匹配项
- Python-如何查找列表中某个元素的所有匹配项?
- zip(* [iter(s)] * n)在Python中如何工作?
- 为什么在Python中将“ dir()”命名为“ dir”?
在python中,如何“如果finditer(...)没有匹配项”?(python finditer)
当finditer()找不到任何东西时,我想做些什么。
import repattern = "1"string = "abc" matched_iter = re.finditer(pattern, string)# <if matched_iter is empty (no matched found>.# do something.# else for m in matched_iter: print m.group()
我能想到的最好的办法是手动跟踪找到的内容:
mi_no_find = re.finditer(r''\w+'',"$$%%%%") # not matching.found = Falsefor m in mi_no_find: print m.group() found = Trueif not found: print "Nothing found"
不回答的相关文章:
- 计算finditer匹配项:正则表达式匹配项的数量(我不需要计算,我只需要知道是否没有匹配项即可)。
- finditer vs match:使用re.finditer和re.match时的行为不同(说总是必须遍历finditer返回的迭代器)
[edit]
-我对枚举或计数总输出没有兴趣。只有找到了,否则找不到动作。
-我知道我可以将finditer放入列表中,但这对于大字符串而言效率不高。一个目标是降低内存利用率。
答案1
小编典典更新了04/10/2020
使用re.search(pattern, string)
来检查,如果存在模式。
pattern = "1"string = "abc"if re.search(pattern, string) is None: print(''do this because nothing was found'')
返回值:
do this because nothing was found
如果你想 遍历返回 ,然后将re.finditer()
内re.search()
。
pattern = ''[A-Za-z]''string = "abc"if re.search(pattern, string) is not None: for thing in re.finditer(pattern, string): print(''Found this thing: '' + thing[0])
返回值:
Found this thing: aFound this thing: bFound this thing: c
因此,如果您想同时使用这两个选项,请将该else:
子句与if re.search()
条件语句一起使用。
pattern = "1"string = "abc"if re.search(pattern, string) is not None: for thing in re.finditer(pattern, string): print(''Found this thing: '' + thing[0])else: print(''do this because nothing was found'')
返回值:
do this because nothing was found
下面的上一个答复(不足,仅在上面阅读)
如果.finditer()与模式不匹配,则它将在相关循环内不执行任何命令。
所以:
- 在 用于迭代正则表达式 的循环之前, 设置变量
- 在用于迭代正则表达式返回的循环 之后(之外) 调用变量
这样,如果正则表达式调用未返回任何内容,则该循环将不会执行,并且循环后的变量调用将返回与设置时完全相同的变量。
下面,示例1演示了正则表达式查找模式。示例2显示了正则表达式找不到模式,因此循环中的变量从未设置。 示例3 显示了我的建议-
在regex循环之前设置变量,因此,如果regex找不到匹配项(随后不触发循环),则循环后的变量调用将返回初始变量集(确认找不到正则表达式模式)。
记住要导入 import re 模块。
示例1(在字符串“ hello world”中搜索字符“ he”将返回“ he”)
my_string = ''hello world''pat = ''(he)''regex = re.finditer(pat,my_string)for a in regex: b = str(a.groups()[0])print(b)# returns ''he''
示例2(在字符串“ hello world”中搜索字符“ ab”不匹配任何内容,因此不会执行“ for a in
regex:”循环,并且不会为b变量分配任何值。)
my_string = ''hello world''pat = ''(ab)''regex = re.finditer(pat,my_string)for a in regex: b = str(a.groups()[0])print(b)# no return
示例3(再次搜索字符“ ab”,但是这次在循环之前将变量b设置为“ CAKE”,然后在循环外部调用变量b返回初始变量-即“ CAKE”),因为循环未执行)。
my_string = ''hello world''pat = ''(ab)''regex = re.finditer(pat,my_string)b = ''CAKE'' # sets the variable prior to the for loopfor a in regex: b = str(a.groups()[0])print(b) # calls the variable after (and outside) the loop# returns ''CAKE''
还值得注意的是,在设计要输入到正则表达式的模式时,请确保使用括号指示组的开始和结束。
pattern = ''(ab)'' # use thispattern = ''ab'' # avoid using this
回到最初的问题:
由于找不到任何内容不会执行for循环(对于regex中的for),用户可以预加载变量,然后在for循环之后检查该变量是否为原始加载值。这将使用户知道是否未找到任何内容。
my_string = ''hello world''pat = ''(ab)''regex = re.finditer(pat,my_string)b = ''CAKE'' # sets the variable prior to the for loopfor a in regex: b = str(a.groups()[0])if b == ‘CAKE’: # action taken if nothing is returned
python – 如何使用不同的数字替换字符串中的所有匹配项
我需要更换它
new_q = q[:q.index('?')] + str(random.randint(1,rand_max)) + q[q.index('?')+1:]
用什么来取代所有的发生?随机,不同的数字.
我怎样才能做到这一点?
解决方法
import re import random a = 'abc?def?ghi?jkl' rand_max = 9 re.sub(r'\?',lambda x:str(random.randint(1,rand_max)),a) # returns 'abc3def4ghi6jkl'
或没有正则表达式:
import random a = 'abc?def?ghi?jkl' rand_max = 9 while '?' in a: a = a[:a.index('?')] + str(random.randint(1,rand_max)) + a[a.index('?')+1:]
Python-如何查找列表中某个元素的所有匹配项?
index()
只会给出列表中第一个出现的项目。有没有整齐的技巧可以返回列表中的所有索引?
答案1
小编典典你可以使用列表理解:
indices = [i for i, x in enumerate(my_list) if x == "whatever"]
答案2
小编典典虽然不是直接解决列表问题的方法,但numpy对于这种情况确实很有帮助:
import numpy as npvalues = np.array([1,2,3,1,2,4,5,6,3,2,1])searchval = 3ii = np.where(values == searchval)[0]
返回:
ii ==>array([2, 8])
与其他一些解决方案相比,这对于包含大量元素的列表(数组)而言可能会更快。
zip(* [iter(s)] * n)在Python中如何工作?
s = [1,2,3,4,5,6,7,8,9]n = 3zip(*[iter(s)]*n) # returns [(1,2,3),(4,5,6),(7,8,9)]
zip(*[iter(s)]*n)
工作如何?如果用更冗长的代码编写,它将是什么样?
答案1
小编典典iter()
是序列上的迭代器。[x] * n
产生一个包含n数量的列表x
,即一个长度的列表n,其中每个元素都是x。*arg
将序列解压缩为函数调用的参数。因此,你要将3次相同的迭代器传递给zip(),并且每次都会从迭代器中提取一个项目。
x = iter([1,2,3,4,5,6,7,8,9])print zip(x, x, x)
为什么在Python中将“ dir()”命名为“ dir”?
在Python中,有一个名为的内置函数dir
。这用于获取对象的所有属性的列表。
我了解它的作用,但对为什么调用它感到困惑dir
。这个名称与从对象获取属性有什么关系?
关于在python中,如何“如果finditer和...没有匹配项”?的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于python – 如何使用不同的数字替换字符串中的所有匹配项、Python-如何查找列表中某个元素的所有匹配项?、zip(* [iter(s)] * n)在Python中如何工作?、为什么在Python中将“ dir()”命名为“ dir”?等相关内容,可以在本站寻找。
本文标签: