我有一个包含子列表的列表,每个子列表包含2个字符串:
cor = [['s', 'urmange'], ['su', 'rmange'], ['sur', 'mange'], ['surm', 'ange'], ['surma', 'nge'], ['surman', 'ge'], ['surmang', 'e']]
现在我想检查子列表中的每两个元素是否存在于名为dic的字典中,如果这两个元素存在
if subL[1] in Dic and subL[2] in Dic:
我的输出如下:
sur mange
否则
print("No match with dic")
如何使用Python实现这一点?
这就是我一直在做的:
for sub_list in cor:
for i in range (0,len(sub_list)):
if sub_list[i] in my_list and sub_list[i+1] in my_list:
print ("R3: You mean:", sub_list)
我得到下一个输出:
R3: You mean: ['sur', 'mange']
出现此错误:
Traceback (most recent call last):
File "<pyshell#88>", line 3, in <module>
if sub_list[i] in my_list and sub_list[i+1] in my_list:
IndexError: list index out of range
我以为我做错了什么!