Python 使全局条件中断
首先,请允许我说,我自己学习python是出于我自己的好奇心,有人向我推荐了一门免费的、公开的在线计算机科学课程,因此,如果我使用的术语不正确,我深表歉意 我以前在这里看到过关于这个特定问题的问题,但我有一个与之不同的问题,我不想劫持这些线程。问题是: 子字符串是另一个字符串中的任何连续字符序列。同一子字符串可能在同一字符串中出现多次:例如,“assesses”子字符串“sses”出现2次,而“trans Panamanian banana”子字符串“an”“6次。编写一个接受两行输入的程序,我们称之为第一个指针和第二个干草堆。打印针作为haystack的子串出现的次数。“ 我的解决方案(有效)是:Python 使全局条件中断,python,variables,conditional,break,conditional-statements,Python,Variables,Conditional,Break,Conditional Statements,首先,请允许我说,我自己学习python是出于我自己的好奇心,有人向我推荐了一门免费的、公开的在线计算机科学课程,因此,如果我使用的术语不正确,我深表歉意 我以前在这里看到过关于这个特定问题的问题,但我有一个与之不同的问题,我不想劫持这些线程。问题是: 子字符串是另一个字符串中的任何连续字符序列。同一子字符串可能在同一字符串中出现多次:例如,“assesses”子字符串“sses”出现2次,而“trans Panamanian banana”子字符串“an”“6次。编写一个接受两行输入的程序,我
first=str(输入())
second=str(输入())
计数=0
位置=0
当位置
如果您注意到,我曾在两个不同的场合发表过if声明,即如果位置小于0,则需要中断。是否有某种方法可以使这成为一种“全局”条件,以便我没有重复代码?我认为随着程序复杂性的提高,效率变得至关重要,因此我现在正在尝试制定良好的实践
python大师会如何优化这段代码,还是我太挑剔了?看看正则表达式,python的
re
模块(http://docs.python.org/library/re.html).例如
import re
first = str(input())
second = str(input())
regex = first[:-1] + '(?=' + first[-1] + ')'
print(len(re.findall(regex, second)))
查看正则表达式,python的
re
模块(http://docs.python.org/library/re.html).例如
import re
first = str(input())
second = str(input())
regex = first[:-1] + '(?=' + first[-1] + ')'
print(len(re.findall(regex, second)))
正如Matthew Adams所提到的,最好的方法是使用python的re模块 对于您的情况,解决方案如下所示:
import re
def find_needle_in_heystack(needle, heystack):
return len(re.findall(needle, heystack))
既然您正在学习python,最好的方法就是使用“干式”[不要重复自己]咒语
要快速了解几个非常重要的python模块,您可以学习以下课程:
这只需要一天时间。正如Matthew Adams所提到的,最好的方法是使用python的re模块 对于您的情况,解决方案如下所示:
import re
def find_needle_in_heystack(needle, heystack):
return len(re.findall(needle, heystack))
既然您正在学习python,最好的方法就是使用“干式”[不要重复自己]咒语
要快速了解几个非常重要的python模块,您可以学习以下课程:
这只需要你一天的时间。甚至你的aproach也可以简化(它使用find返回-1的事实,而你让它从不存在的偏移量进行搜索):
甚至您的aproach也可以简化(它使用find返回-1的事实,而您让它从不存在的偏移量进行搜索):
我认为Matthew和darshan有最好的解决方案。我将根据您的解决方案发布一个变体:
first = str(input())
second = str(input())
def count_needle(first, second):
location = str.find(second,first)
if location == -1:
return 0 # none whatsoever
else:
count = 1
while location < len(second):
location = str.find(second,first,location +1)
if location < 0:
break
count = count + 1
return count
print(count_needle(first, second))
first=str(输入())
second=str(输入())
def计数针(第一、第二):
location=str.find(第二个,第一个)
如果位置==-1:
返回0#无任何内容
其他:
计数=1
当位置
想法:
- 适当时,使用函数构造代码
- 在进入while循环之前,初始化变量
,以避免多次检查位置<0location
first = str(input())
second = str(input())
def count_needle(first, second):
location = str.find(second,first)
if location == -1:
return 0 # none whatsoever
else:
count = 1
while location < len(second):
location = str.find(second,first,location +1)
if location < 0:
break
count = count + 1
return count
print(count_needle(first, second))
first=str(输入())
second=str(输入())
def计数针(第一、第二):
location=str.find(第二个,第一个)
如果位置==-1:
返回0#无任何内容
其他:
计数=1
当位置
想法:
- 适当时,使用函数构造代码
- 在进入while循环之前,初始化变量
,以避免多次检查位置<0location
- 给你:
first = str(input())
second = str(input())
x=len(first)
counter=0
for i in range(0,len(second)):
if first==second[i:(x+i)]:
counter=counter+1
print(counter)
给你:
first = str(input())
second = str(input())
x=len(first)
counter=0
for i in range(0,len(second)):
if first==second[i:(x+i)]:
counter=counter+1
print(counter)
回答
needle=input()
haystack=input()
counter=0
for i in range(0,len(haystack)):
if(haystack[i:len(needle)+i]!=needle):
continue
counter=counter+1
print(counter)
回答
needle=input()
haystack=input()
counter=0
for i in range(0,len(haystack)):
if(haystack[i:len(needle)+i]!=needle):
continue
counter=counter+1
print(counter)
在两个单独的连接处中断没有错。也就是说,完成循环退出的一个简单方法是将代码封装在函数中并使用return。在两个单独的连接处中断没有错。也就是说,完成循环退出的一个简单方法是将代码封装在函数中并使用return。虽然正则表达式通常很好,但
findall
方法不适合此问题。请使用“评估”示例尝试您的代码,它将只看到一个匹配项。原因是某些匹配项可能会重叠,而正则表达式通常不允许重叠。@Blckknght良好调用。我将对其进行编辑,以包含一个前瞻(虽然这有点不对劲)。为了澄清这些评论对未来读者的意义,我最初的答案实际上是regex=first
,而不是当前的regex,这导致了Blckknght指出的问题