我有下面这个字典,我把它变成一个列表:
alphabet_dict_lowerCase = {'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4, 'g': 7, 'f': 6, 'i': 9, 'h': 8, 'k': 11, 'j': 10, 'm': 13, 'l': 12, 'o': 15, 'n': 14, 'q': 17, 'p': 16, 's': 19, 'r': 18, 'u': 21, 't': 20, 'w': 23, 'v': 22, 'y': 25, 'x': 24, 'z': 26}
列表(alphabet_dict_lowerCase):
['a', 'c', 'b', 'e', 'd', 'g', 'f', 'i', 'h', 'k', 'j', 'm', 'l', 'o', 'n', 'q', 'p', 's', 'r', 'u', 't', 'w', 'v', 'y', 'x', 'z']
我想看看intersectionLetter
是否在list(alphabet_dict_lowerCase
列表中使用index()
:
for line in data:
first_half = line[:len(line)//2]
second_half = line[len(line)//2:]
first_half = [str(line) for line in first_half if line.strip()]
second_half = [str(line) for line in second_half if line.strip()]
newList = []
for element in second_half:
if element in first_half:
newList.append(element.split())
intersectionLetter = ''.join(map(str, newList))
print(intersectionLetter)
print(str(newList))
if intersectionLetter in list(alphabet_dict_lowerCase).index(intersectionLetter):
print(intersectionLetter)
十字路口字母打印:['f']
然而,当我检查。index看看['f']
是否在列表中,它告诉我它不在那里,当我可以看到它在那里…为什么会发生这种情况?
就是这样写的,所以['f']
是一个单元素列表。您正在检查这个元素列表是否在另一个列表中,但它不在。提取这个元素并重新赋值,如下所示:
intersectionLetter = intersectionLetter[-1]
然后你会检查f
str是否在列表中,我猜它应该在那里。
只是给你展示一个正在发生的事情的例子:
chars = ["s","r","t"]
s_element = "s"
s_list = ["s"]
s_element in chars <- this evaluates to True
s_list in chars <- this evaluates to False