我有一个列表。
我需要在列表中添加一个词,但我不知道该怎么做。
例如,
我有一个列表 = ['Alice', 'Amy', 'Andy', 'Betty', 'Eric', 'Peter', 'Richard', 'Tony']
最佳答案
您可以使用专业图书馆,例如 sortedcontainers
,这比每次插入后的简单 list.sort
更有效。 SortedList.add
的复杂性是 ~O(log n)。
from sortedcontainers import SortedList
lst = SortedList(['Alice', 'Amy', 'Andy', 'Betty', 'Eric', 'Peter', 'Richard', 'Tony'])
lst.add('Beatrice')
print(lst)
SortedList(['Alice', 'Amy', 'Andy', 'Beatrice', 'Betty', 'Eric', 'Peter', 'Richard', 'Tony'])
关于python - 如何在 python 中将单词插入列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50827262/