Python converting a list to set, big O

人盡茶涼 提交于 2020-05-13 13:37:52

问题


and thanks for help

words = [....#Big list of words]
words_set = set(words)

I have hard time determine what is the complexity of set(words) when n=len(words). Is it O(n) since it moves on all the items of the list, or O(l(n-l)) when l is a single word length? Thanks for help! If there is a difference between WC and BC too.

Edit: don't mind O(l(n-l)) it's mistake for repeating substring big O.


回答1:


I don't understand your second option, but iterating a list is O(n) and you must iterate the list to convert it to a set. Any operation on each element - eg hashing - is a constant factor which is dominated by the linear time of iteration.



来源:https://stackoverflow.com/questions/27824901/python-converting-a-list-to-set-big-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!