草庐IT

Re-Parameters

全部标签

python - 类型错误 : object() takes no parameters

我的代码生成以下错误:TypeError:object()takesnoparametersclassGraph(object):defvertices(self):returnlist(self.__graph_dict.keys())if__name__=="__main__":g={"a":["d"],"b":["c"],"c":["b","c","d","e"],"d":["a","c"],"e":["c"],"f":[]}graph=Graph(g)print("Verticesofgraph:")print(graph.vertices())有什么办法可以解决这个问题吗?

python - Python 正则表达式中的错误? (re.sub with re.MULTILINE)

我注意到Python的Regex库中有一些奇怪的行为,我不确定我是否做错了什么。如果我使用re.sub()和re.MULTILINE在其上运行正则表达式。它似乎只取代了前几次。如果我关闭re.MULTILINE、使用re.subn(...,count=0,flags=re.MULTILINE)或编译正则表达式,它会替换所有出现的地方使用re.compile(...,re.MULTILINE)。我在Ubuntu12.04上运行Python2.7。我已经发布了一个随机示例:Pastebin.com-终端输出codepad-脚本,确认行为(re.subn()除外,它在2.5上有所不同)有人可

python - re.findall 的结果顺序有保证吗?

re.findall返回的匹配列表是否始终与它们在源文本中的顺序相同? 最佳答案 是的,如re模块中所述docs:Returnallnon-overlappingmatchesofpatterninstring,asalistofstrings.Thestringisscannedleft-to-right,andmatchesarereturnedintheorderfound. 关于python-re.findall的结果顺序有保证吗?,我们在StackOverflow上找到一个类似

python - 是否有与 Python 的 re.findall/re.finditer(迭代正则表达式结果)等效的 Perl?

在Python中编译的正则表达式模式haveafindallmethod执行以下操作:Returnallnon-overlappingmatchesofpatterninstring,asalistofstrings.Thestringisscannedleft-to-right,andmatchesarereturnedintheorderfound.Ifoneormoregroupsarepresentinthepattern,returnalistofgroups;thiswillbealistoftuplesifthepatternhasmorethanonegroup.Emp

Python 和 "re"

我有一个关于Regexinpython的教程解释了如何在python中使用re模块,我想从A标签中获取URL,所以知道Regex我写了正确的表达式并在我选择的regex测试应用程序中测试了它并确保它有效。当放入python时它失败了:result=re.match("a_regex_of_pure_awesomeness","astringcontainingtheawesomeness")#resultisNone`经过多次摸索,我发现了这个问题,它会自动期望您的模式位于字符串的开头。我找到了修复方法,但我想知道如何更改:regex=".*(a_regex_of_pure_aweso

python - 类型错误 : object() takes no parameters - but only in Python 3

我正在将一些代码从Python2迁移到Python3,但出现了不同的行为。浏览“更改内容”列表并没有指出任何相关差异,但大概我错过了一个重大差异。我已经尽可能地简化了我的代码以获得这个“最小错误程序”:defdecorator(Type):"""Thisisaclassdecorator.Itreplacesaclasswithasubclasswhich*shouldbe*equivalent.TheresultworksonPython2.7butnotonPython3.4."""classFactorySubclass(Type):"""Thissubclassesfromth

python - 一起使用 re.MULTILINE 和 re.DOTALL python

基本上输入文件是这样的:>U51677Humannon-histonechromatinproteinHMG1(HMG1)gene,completecds.#somerecordsdon'thavethisline(seebelow)Length=2575(sometext)>U51677Humannon-histonechromatinproteinHMG1(HMG1)gene,completeLength=2575(sometext)(etc...)现在我写这个来提取以>开头的行和长度的数字importreregex=re.compile("^(>.*)\r\n.*Length\s

python - 如何在 Python 中使用正则表达式 re.sub() 一个可选的匹配组?

我的问题很简单。我有一个URL,有时它以特定字符结尾。如果它们存在,我想将它们添加到我的新URL。test1="url#123"test2="url"r=re.sub(r"url(#[0-9]+)?",r"new_url\1",test1)#Expectedresult:"new_url#123"#Actualresult:"new_url#123"r=re.sub(r"url(#[0-9]+)?",r"new_url\1",test2)#Expectedresult:"new_url"#Actualresult:"error:unmatchedgroup"当然,我不能只做re.sub

Python 自省(introspection) : description of the parameters a function takes

是否有一个类似于dir()的模块工具可以告诉我给定函数需要哪些参数?例如,我想做一些像dir(os.rename)这样的事情,让它告诉我记录了哪些参数,这样我就可以避免在线检查文档,而是只使用Python脚本接口(interface)做这个。 最佳答案 我知道您对help(thing)或thing.__doc__更感兴趣,但是如果您尝试进行程序化自省(introspection)(而不是人类-可读文档)来了解调用函数,然后你可以使用inspectmodule,如thisquestion中所讨论.

python - 使用 re.findall 查找前 x 个匹配项

我需要限制re.findall找到前3个匹配项然后停止。例如text='some1text2bla3regex4python5're.findall(r'\d',text)然后我得到:['1','2','3','4','5']我想要:['1','2','3'] 最佳答案 re.findall返回一个列表,所以最简单的解决方案就是使用slicing:>>>importre>>>text='some1text2bla3regex4python5'>>>re.findall(r'\d',text)[:3]#Getthefirst3item