Python 将列表转换为集合,大 O

nad*_*dir 6 python big-o set asymptotic-complexity python-3.x

感谢您的帮助

words = [....#Big list of words]
words_set = set(words)
Run Code Online (Sandbox Code Playgroud)

当 n=len(words) 时,我很难确定 set(words) 的复杂性是多少。是 O(n) 因为它在列表的所有项目上移动,还是 O(l(nl)) 当 l 是单个单词长度时?感谢帮助!如果WC和BC之间也有区别的话。

编辑:不要介意 O(l(nl)) ,重复子串大 O 是错误的。

Dan*_*man 6

我不明白你的第二个选择,但迭代列表是 O(n) 并且你必须迭代列表才能将其转换为集合。每个元素上的任何操作(例如散列)都是一个常数因子,由迭代的线性时间决定。