【问题标题】:Python converting a list to set, big OPython将列表转换为集合,大O
【发布时间】:2015-01-07 17:16:27
【问题描述】:

感谢您的帮助

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

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

编辑:不要介意 O(l(n-l)) 重复子字符串大 O 是错误的。

【问题讨论】:

  • 我不认为“字长”很重要,或者更确切地说,假设所有字的长度相同,它应该是一个恒定的影响,因此微不足道。

标签: python python-3.x set big-o asymptotic-complexity


【解决方案1】:

我不明白你的第二个选项,但是迭代一个列表是 O(n),你必须迭代这个列表才能将它转换为一个集合。对每个元素的任何操作(例如散列)都是一个常数因素,受迭代的线性时间支配。

【讨论】:

猜你喜欢
  • 2013-12-16
  • 2017-02-27
  • 1970-01-01
  • 2011-01-29
  • 1970-01-01
  • 2014-11-24
  • 1970-01-01
  • 2017-02-06
  • 2011-09-29
相关资源
最近更新 更多