我有一长串难以破译的文本,每一行都用括号隔开(只包括一行,因为我无法让这个程序在哪怕一行上运行):
"Thyroid Disorders Understanding Concepts Kaplan Endocrine Focused Review Tests n/a 88% (35/40)"
我正试图将其格式化为这样,并将其附加到一个文件中:
"Thyroid Disorders Understanding Concepts 88% (35/40)"
因此,我需要从每个字符串中删除字符串“Kaplan”、“endometric”、“Focused”、“Review”、“Tests”和“n/a”,并去掉制表符/换行符
这是我的密码:
text = """Thyroid Disorders Understanding Concepts Kaplan Endocrine A Focused Review Tests n/a 88% (35/40)
"""
line = ''
for character in text:
line = line + character # append every character to string
if character == ')': # closing parenthesis signals end of one line
print('Original line: '+ line) # sanity check
line_as_list = line.split() # removes tabs/newlines and makes it easier to remove certain strings
for word in line_as_list: # loop through each list item, remove if needed
if word == 'Kaplan':
line_as_list.remove(word)
print(line_as_list) # another sanity check, 'Kaplan' is gone
if word == 'Endocrine': # never runs
line_as_list.remove(word)
print(line_as_list )
# Intentionally left out the rest of the words that need to be removed
这将返回以下内容:
"Original line: Thyroid Disorders Understanding Concepts Kaplan Endocrine A Focused Review Tests n/a 88% (35/
40)"
['Thyroid', 'Disorders', 'Understanding', 'Concepts', 'Endocrine', 'A', 'Focused', 'Review', 'Tests',
'n/a', '88%', '(35/40)']
第一个if
语句下的代码执行我想要的方式,但是if word == 'Endocrine'
下的代码块永远不会运行
我试过了
if word == 'Kaplan' or word == 'Endocrine':
line_as_list.remove(word)
及
if word == 'Kaplan':
line_as_list.remove(word)
elif word == 'Endocrine':
line_as_list.remove(word)
两者都没有效果,“卡普兰”是唯一被删除的词。谢谢你在这方面的帮助
问题描述
问题是您正在对当前迭代的列表进行变异。由于
Kaplan
和Endocrine
紧随其后,内分泌将被跳过,因为它接管了Kaplan
的索引,循环将继续到下一个索引(即内分泌的旧索引)。如果您在自己的代码中在Kaplan和endometric之间添加另一个字符串,则很容易说明这一点,您将看到这两个字符串都被删除,因为中间的单词将被跳过解决方案
最佳做法是创建一个新的列表,而不删除要删除的项目,而不是修改输入列表
我建议使用列表理解来解决这个问题,并创建一个新的列表
更改此行:
致:
这样,当您从原始列表中删除“Kaplan”时,它不会影响列表上的迭代
这里出现错误的原因是remove将所有元素向后拉一步,迭代器不会更新,因此在甲状腺被移除后,内分泌处于它的位置,不再被触发。 一个简单的解决办法是:
相关问题 更多 >
编程相关推荐