[英]How can I match element by element of list?
i'm trying to solve the problem of checking the distance between letters by looking at the alphabet.我正在尝试通过查看字母来解决检查字母之间距离的问题。 I described it in a dictionary.
我在字典里描述过。 I gave "l = 10000" so that later I could easily distinguish the correct numerator.
我给出了“l = 10000”,以便稍后我可以轻松区分正确的分子。 I think the idea itself is good, but it gives me the error "if abs (words [text [i] [j]] - words [text [i] [j + 1]] <10): IndexError: string index out of range "
我认为这个想法本身很好,但它给了我错误“如果 abs (words [text [i] [j]] - words [text [i] [j + 1]] <10): IndexError: string index out范围"
I will be grateful for every tip.我将不胜感激每一个提示。
Code:代码:
words={'A':0,'B':1,'C':2,'D':3,'E':4,'F':5,'G':6,'H':7,'I':8,'J':9,'K':10,'L':11,'M':12,'N':13,'O':14,'P':15,'Q':16,'R':17,'S':18,'T':19,'U':20,'V':21,'W':22,'X':23,'Y':24,'Z':25,}
text = ['XXUXXTYVXWA', 'YTWYVWUTYA', 'PUOMQVMRNOSNVONOQOQMPPMRTVRSVRUNMOUSVUOTTMNRVQX']
l = 0
t = []
for i in range(0,len(text)):
for j in range(0,len(text[i])):
if abs(words[text[i][j]] - words[text[i][j+1]] < 10):
l = l+1
else:
l = 10000
t.append(l)
l = 0
print(t)
The error is raised with the last iteration, when you want to compare the last letter with the letter after the last letter, which doesn't exist.当您想将最后一个字母与最后一个字母之后的字母进行比较时,最后一次迭代会引发错误,该字母不存在。 Perhaps start at 1 and compare the current letter with the previous letter like this:
也许从 1 开始,将当前字母与前一个字母进行比较,如下所示:
words={'A':0,'B':1,'C':2,'D':3,'E':4,'F':5,'G':6,'H':7,'I':8,'J':9,'K':10,'L':11,'M':12,'N':13,'O':14,'P':15,'Q':16,'R':17,'S':18,'T':19,'U':20,'V':21,'W':22,'X':23,'Y':24,'Z':25,}
text = ['XXUXXTYVXWA', 'YTWYVWUTYA', 'PUOMQVMRNOSNVONOQOQMPPMRTVRSVRUNMOUSVUOTTMNRVQX']
l = 0
t = []
for i in range(0,len(text)):
for j in range(1,len(text[i])):
if abs(words[text[i][j-1]] - words[text[i][j]] < 10):
l = l+1
else:
l = 10000
t.append(l)
l = 0
print(t)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.