假设我有一个包含数千个单词的单词列表。但是,它们的长度都一样。比如说,所有5个字母的单词,我想找到所有中间字母为"o"的单词(第三个位置,索引中的第二个位置,因为它从0开始(。
我该如何删除原始列表中的所有其他单词,以便从中删除它?
我会多次这样做,在这种情况下最多4次,因为单词都是5个字母。
所以它就像original_list=["房子","鬼","那里","松散"]之类的东西。我的输出应该是original_list=["ghost","loose"]
理想情况下,我宁愿从原始列表中删除不匹配的项目,而不是附加到新列表中。这样,我就可以在每次迭代中不断地修剪它。
也许可以尝试迭代方法?
from typing import List
def remove_words(words: List[str]):
index = 0
while index < len(words):
if words[index][2] != 'o':
words.pop(index)
else:
index += 1
# return words -- OPTIONAL
if __name__ == "__main__":
original_list = ["house", "ghost", "there", "loose"]
remove_words(original_list)
print(original_list) # ['ghost', 'loose']
绩效说明
虽然上面的解决方案回答了OP,但它不是最具性能的,也不是"最佳";纯函数";因为它改变了外部作用域状态(https://en.wikipedia.org/wiki/Pure_function)。
from timeit import timeit
def remove_words():
# defining the list here instead of global scope because this is an impure function
words = ["house", "ghost", "there", "loose"]
index = 0
while index < len(words):
if words[index][2] != 'o':
words.pop(index)
else:
index += 1
def remove_words_with_list_comprehension():
# defining the list here to ensure fairness when testing performance
words = ["house", "ghost", "there", "loose"]
return [word for word in words if word[2] == 'o']
if __name__ == "__main__":
t1 = timeit(lambda : remove_words_with_list_comprehension(), number=1000000)
t2 = timeit(lambda : remove_words(), number=1000000)
print(f"list comprehension: {t1}")
print(f"while loop: {t2}")
# OUTPUT
# list comprehension: 0.40656489999673795
# while loop: 0.5882093999971403