[英]Find the next iteration in loop python
I am trying to find out if given a string if 2 target characters follow one another. 我试图找出一个字符串,如果2个目标字符相互跟随。 So essentially, I am trying to find if a character and its neighbor are target characters.
基本上,我试图找出一个角色及其邻居是否是目标角色。 How should I go about this?
我该怎么办呢? The following is what I have tried so far.
以下是我到目前为止所尝试的内容。
target_list=["t","a","r","g","e","t"]
for char in some_string:
if (char and some_string[some_string.index(char)+1]) in target_list:
print ("correct")
else:
print ("incorrect")
Expected output: 预期产量:
Just go through the indices and process every pair of characters: 只需浏览索引并处理每对字符:
for i in range(len(some_string) - 1):
if some_string[i] in target_list and some_string[i+1] in target_list:
print ("correct")
break
if i == len(some_string) - 1:
print ("incorrect")
You can alternatively create a mapping and look for adjacent true positives: 您也可以创建映射并查找相邻的正数:
m = [(char in target_list) for char in some_string]
for i in range(len(m) - 1):
if m[i] and m[i+1]:
print ("correct")
break
if i == len(m) - 1:
print ("incorrect")
from itertools import tee
def rolling_window(iterable, n=2):
iterators = tee(iterable, n)
for i, iterator in enumerate(iterators):
for skip in range(i):
next(iterator, None)
return zip(*iterators)
def match(some_string, target, n=2):
target = set(target)
return any(target.issuperset(s) for s in rolling_window(some_string, n=n))
Just use map: 只需使用地图:
target_list=['t','a','r','g','e']
def toDict(list):
mp = {}
for c in list:
mp[c] = True
return mp
d = toDict(target_list)
print("dict:" , d)
def check(string, mp):
count = 0
for c in string:
if(mp.get(c,False)):
count = count+1
if(count > 1):
return True
else:
count = 0
return False
print("check:" , check("heytr", d))
print("check:" , check("hyt", d))
print("check:" , check("heyt", d))
A regular expression solution. 正则表达式解决方案。
import re
target_chars='target'
p = re.compile('[%s]{2}' % re.escape(target_chars))
m = p.search(some_string)
if m:
print('correct')
else:
print('incorrect')
You can also use any()
and zip()
for this: 你也可以使用
any()
和zip()
:
target_list=["t","a","r","g","e","t"]
target_list = set(target_list)
some_strings = ["heytr", "hyt", "heyt"]
def match_strings(string, target_list):
return any(x in target_list and y in target_list for x, y in zip(string, string[1:]))
for some_string in some_strings:
if match_strings(some_string, target_list):
print("correct")
else:
print("incorrect")
Which Outputs: 哪些输出:
correct
incorrect
incorrect
Logic of above code : 上述代码的逻辑 :
target_list
to a set, since set lookup is constant time. target_list
转换为集合,因为set lookup是常量时间。 If you keep it as a list, then lookup time is linear. zip()
to create pairs of the current element and the next element, and checks if both of these elements exist in target_list
. zip()
创建当前元素和下一个元素的对,并检查target_list
是否存在这两个元素。 Then checks if any()
of the pairs exist, which returns True
if any of the pairs exist, and False
if none exist. any()
对any()
,如果存在any()
对,则返回True
,如果不存在则返回False
。 some_strings
, and check the above for each one. some_strings
所有字符串,并检查上面的每个字符串。 def judge(some_string):
for index in range(0,len(some_string)-1):
if some_string[index] in target_list and some_string[index+1] in target_list:
print("correct")
break
else:
print("incorrect")
judge("heytr") --correct
judge("hyt") -- incorrect
judge("heyt") --incorrect
For loop iterates on all character of the string checks if i and i+1 is present in target.if yes prints out correct. For循环迭代字符串的所有字符检查i和i + 1是否存在于target.if是打印出正确。 after all iterations of loop is over if no two character are found in target it comes to the else part of for loop and prints out incorrect.
在循环的所有迭代结束后,如果在目标中找不到两个字符,它将进入for循环的else部分并打印出错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.