简体   繁体   English

列表列表的索引的复杂度是多少(例如list [x] [y])

[英]What is the complexity of index for lists of lists(e.g. list[x][y])

In python 在python中

lst = [[1,2,3...],[4,5,6...]]

Assume I have a list whose length is n and each nested list has the same length. 假设我有一个长度为n的列表,每个嵌套列表的长度都相同。 So I want to use lst[x][y] to find some element. 所以我想用lst[x][y]查找一些元素。 what is the complexity of it? 它的复杂性是什么? I tried to google it but not find answers. 我试图用谷歌搜索,但没有找到答案。

Edited:Thanks guys! 编辑:谢谢大家! So if I input a list containing n lists (each containing n elements), then how many times do I index the list (in terms of n)? 因此,如果我输入一个包含n个列表的列表(每个列表包含n个元素),那么该索引列表多少次(以n表示)? Is it 2n? 是2n吗?

Time complexity of Python data structures are documented here: Python数据结构的时间复杂性在此处记录:

https://wiki.python.org/moin/TimeComplexity https://wiki.python.org/moin/TimeComplexity

The one you're asking about is "Get Item" for list , and it's O(1) . 您要询问的是“获取项目” list ,它是O(1) Even though you index a list and a nested list, that's still O(1) . 即使您为列表和嵌套列表建立索引,它仍然是O(1)

Indexing is an O(1) operation (constant time). 索引是O(1)操作(恒定时间)。 Thus, two successive indexing operations is still O(1) . 因此,两个连续的索引操作仍为O(1)

This page shows that "get item" is O(1) . 此页面显示“获取项目”为O(1) To analyze an expression like l[x][y] it might be helpful to break it into its components: 要分析像l[x][y]这样的表达式,将其分解成各个部分可能会有所帮助:

temp = l[x]
temp[y]

When analyzing sequential operations, we add the time complexity. 在分析顺序操作时,我们增加了时间复杂度。 So this is O(1) + O(1) . 所以这是O(1) + O(1) But this simplifies to just O(1) . 但这简化为O(1)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:如何使用列表在列表之间进行复制,例如 list1[index1] = list2[index2] - Python: how to copy between lists using lists, e.g. list1[index1] = list2[index2] 如何在 Python 中创建 x 列表(例如使用 while 循环)? - How do I create x lists in Python (e.g. with a while-loop)? 在Python词典中引用mutable(例如,列表)作为值 - 什么是最佳实践? - References to mutables (e.g., lists) as values in Python dictionaries - what is best practice? 将字典与不可散列或不可比较的值进行比较? (例如列表或数据框) - Compare Dictionaries with unhashable or uncomparable values? (e.g. Lists or Dataframes) 列表接受可迭代对象作为输入。 但是,当输入为int或float时,例如list(1),则不接受此输入。 为什么? - Lists accept iterable objects as inputs. However, when the input is an int or float, e.g. list(1), this is not accepted. Why? 从列表中获取(x,y)坐标 - getting (x,y) coordinates from a list of lists 在 Python 的 for 循环中使用 range(len(x)) 时添加索引有什么影响? 例如范围(len(x [0]) - What is the effect of adding an index when using range(len(x)) in a for loop in Python? e.g. range(len(x[0]) 从[X]和[Y]的单独列表中列出[x,y]点的列表 - Making a list of [x, y] points from separate lists of [X] and [Y] 操作 pandas 数据框列中的列表(例如,除以另一列) - Manipulate lists in a pandas data frame column (e.g. divide by another column) 自动查找并添加坐标以在由不均匀列表字典制成的箱线图上添加注释(例如计数) - Automatically find and add the coordinates to add Annotations (e.g. count) on a Boxplot made from a Dictionary of uneven Lists
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM