[英]How to iterate through list
I have a list that contains some 20 number of strings , and another list that contains 5 number of strings , I want to check if the list of 5 strings can be found in the set of 20 strings list below is my code 我有一个包含20个字符串的列表,另一个包含5个字符串的列表,我想检查一下是否可以在下面的20个字符串集中找到5 个字符串的列表。
correct_response = []
incorrect_response = []
elements_text = elements1_file.readline().strip()
for ele in elements_text:
while elements_text:
if ele == quiz_test:
correct_response.append(ele)
elif ele != quiz_test:
incorrect_response.append(ele)
else:
pass
elements_text = elements1_file.readline().strip()
print(correct_response,incorrect_response)
Now, the correct response and incorrect cannot be printed, what did I do wrong. 现在,无法打印正确的响应和错误的内容,我做错了什么。
is this what you are looking for? 这是你想要的? I am assuming that quiz_test
is the collection of 5 strings also it appears you should use elements1_file.readlines()
instead of elements1_file.readline()
and use .strip()
on the strings iterated over. 我假设quiz_test
是5个字符串的集合,而且看来您应该使用elements1_file.readlines()
而不是elements1_file.readline()
并在迭代的字符串上使用.strip()
。 I believe your code was iterating over each letter of the first line of the file, instead of each line consecutively. 我相信您的代码正在遍历文件第一行的每个字母,而不是连续地遍历每一行。
correct_response = []
incorrect_response = []
elements_text = elements1_file.readline()
for ele in elements_text:
if ele.strip() in quiz_test:
correct_response.append(ele)
else:
incorrect_response.append(ele)
print(correct_response,incorrect_response)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.