繁体   English   中英

在循环python中找到下一个迭代

[英]Find the next iteration in loop python

我试图找出一个字符串,如果2个目标字符相互跟随。 基本上,我试图找出一个角色及其邻居是否是目标角色。 我该怎么办呢? 以下是我到目前为止所尝试的内容。

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")

预期产量:

  1. 如果some_string =“heytr”==“正确”
  2. 如果some_string =“hyt”==“不正确”
  3. 如果some_string =“heyt”==“不正确”

只需浏览索引并处理每对字符:

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")

您也可以创建映射并查找相邻的正数:

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))

只需使用地图:

    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)) 

正则表达式解决方案。

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')

你也可以使用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")

哪些输出:

correct
incorrect
incorrect

上述代码的逻辑

  • target_list转换为集合,因为set lookup是常量时间。 如果将其保留为列表,则查找时间是线性的。
  • 使用zip()创建当前元素和下一个元素的对,并检查target_list是否存在这两个元素。 然后检查是否存在any()any() ,如果存在any()对,则返回True ,如果不存在则返回False
  • 然后遍历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循环迭代字符串的所有字符检查i和i + 1是否存在于target.if是打印出正确。 在循环的所有迭代结束后,如果在目标中找不到两个字符,它将进入for循环的else部分并打印出错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM