Python 属性错误:非类型没有属性';集团';
它正在搜索的字符串是:“5'、'、5'、'、6'、'、3'、”。在两个不同的在线正则表达式生成器上,上述正则表达式对字符串非常有效。为什么它适用于X坐标而不适用于Y坐标?代码完全相同 (顺便说一下,我正试图从字符串中获取整数)Python 属性错误:非类型没有属性';集团';,python,regex,attributeerror,nonetype,Python,Regex,Attributeerror,Nonetype,它正在搜索的字符串是:“5'、'、5'、'、6'、'、3'、”。在两个不同的在线正则表达式生成器上,上述正则表达式对字符串非常有效。为什么它适用于X坐标而不适用于Y坐标?代码完全相同 (顺便说一下,我正试图从字符串中获取整数) 谢谢变量str1的内容是什么 re.match(r'\d+',number)返回None,因为number与正则表达式不匹配,请查看变量result的内容,该变量取决于str1 每个正则表达式都必须一步一步地进行测试,请尝试一些regexweb工具来测试它们 将正则表达式
谢谢变量str1的内容是什么
re.match(r'\d+',number)
返回None
,因为number
与正则表达式不匹配,请查看变量result
的内容,该变量取决于str1
每个正则表达式都必须一步一步地进行测试,请尝试一些regexweb工具来测试它们
将正则表达式更改为:regex=“\b([0-9]+)\,\s\b”
无需正则表达式:
xcoord = []
regex = ur"\b[0-9,]{1,}[,]\s\b" #regex for x coordinates
result = re.findall(regex,str1)
for number in result: #get x numbers from coordinate
match = int(re.match(r'\d+', number).group())
xcoord.append(match) #now got array of numbers for x
maxValueX = max(xcoord) #biggest x value
ycoord = []
regex = ur"\b[,]\s[0-9,]{1,}\b" #regex for y coordinates
result = re.findall(regex,str1)
for number in result: #get y numbers from coordinate
match = int(re.match(r'\d+', number).group())
ycoord.append(match) #now got array of numbers for y
maxValueY = max(ycoord) #biggest y value
print maxValueX
print maxValueY
这将创建两个列表,一个包含所有第一个元素,另一个包含所有第二个元素
str1 = "(8, 5)(9, 5)(10, 5)(11, 5)(14, 5)(17, 5)(20, 5)(21, 5)(22, 5)(23, 5)(1, 6)(5, 6)(8, 6)(9, 6)(10, 6)(11, 6)(14, 6)(17, 6)(20, 6)(23, 6)(1, 7)(5, 7)(8, 7)(9, 7)(14, 7)(17, 7)(20, 7)(21, 7)(22, 7)(23, 7)(1, 8)(5, 8)(8, 8)(9, 8)(10, 8)(11, 8)(14, 8)(17, 8)(20, 8)(21, 8)(22, 8)(23, 8)"
xcoord = [int(element.split(",")[0].strip()) for element in str1[1:-1].split(")(")]
ycoord = [int(element.split(",")[1].strip()) for element in str1[1:-1].split(")(")]
maxValueX = max(xcoord); maxValueY = max(ycoord)
print maxValueX;
print maxValueY;
x_cord=map(int,re.findall(“(?顺便说一句,如果有人问,Y或x的第一个正则表达式是完美的,并且可以工作,它是第二个带有.group()的正则表达式,这是导致问题的原因。这是如何“非常复杂的”[sic]“?显然,数字
中的模式
没有匹配
-为什么不打印数字
,并找出原因?-1您当然可以提供一个完整的程序来说明问题。这样我们实际上就有了您的输入数据。显然re.match()
返回了None
。但是您的程序依赖于它的输入数据。您没有提供这些数据。re.match()在某些情况下返回None“print number”给出了,2,2,2,2,2,2,2,3,3,3,3,3,4,4,4”,正则表达式“\d+”应该找到编号?str1=(8,5)(10,5)(11,5)(14,5)(17,5)(20,5)(21,5)(22,5)(23,5)(1,6)(5)(5,6)(9,6)(10,6)(11,6)(14,6)(17,6)(20,6)(23,6)(1,7)(5,7)(8,7)(9,7)(14,7)(17,7)(20,7)(21,7)(22,7)(23,7)(1,8)(5,8)(8,8)(9,8)(10,8)(11,8)(14,8)(17,8)(20,8)(21,8)(22,8)(23,8),我正试图将括号中的X值作为第一个数字,Y作为第二个数字,分成单独的整数数组。将您的正则表达式改为“”([0-9]+)\,\s\b“我在我提供的字符串、str1和result上使用了完全相同的regex web工具,但仍然给出了错误。juankysmith-您的解决方案与我的完全匹配。谢谢您的解决方案不,您的include”“完美,有效地减少了我的代码!先生,您赢得了一个绿色的勾号
str1 = "(8, 5)(9, 5)(10, 5)(11, 5)(14, 5)(17, 5)(20, 5)(21, 5)(22, 5)(23, 5)(1, 6)(5, 6)(8, 6)(9, 6)(10, 6)(11, 6)(14, 6)(17, 6)(20, 6)(23, 6)(1, 7)(5, 7)(8, 7)(9, 7)(14, 7)(17, 7)(20, 7)(21, 7)(22, 7)(23, 7)(1, 8)(5, 8)(8, 8)(9, 8)(10, 8)(11, 8)(14, 8)(17, 8)(20, 8)(21, 8)(22, 8)(23, 8)"
xcoord = [int(element.split(",")[0].strip()) for element in str1[1:-1].split(")(")]
ycoord = [int(element.split(",")[1].strip()) for element in str1[1:-1].split(")(")]
maxValueX = max(xcoord); maxValueY = max(ycoord)
print maxValueX;
print maxValueY;
x_cord = map(int,re.findall("(?<=\()\d+",str1))
y_cord= map(int,re.findall("(?<=\,\s)\d+",str1))
x_cord
[8, 9, 10, 11, 14, 17, 20, 21, 22, 23, 1, 5, 8, 9, 10, 11, 14, 17, 20, 23, 1, 5, 8, 9, 14, 17, 20, 21, 22, 23, 1, 5, 8, 9, 10, 11, 14, 17, 20, 21, 22, 23]
y_cord
[5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8]