Python 3.x 列表旁边的列表是什么意思?

Python 3.x 列表旁边的列表是什么意思?,python-3.x,Python 3.x,我对列表[外部索引][内部索引]的作用感到困惑?我认为当两个列表相邻时,意味着第一个列表是选定列表,第二个列表表示第一个列表的索引。然而,这里的情况似乎并非如此 def flatten(lists): results = [] for outer_index in range(len(lists)): # outer index = 0, 1 for inner_index in range(len(lists[outer_index])): # inner_in

我对列表[外部索引][内部索引]的作用感到困惑?我认为当两个列表相邻时,意味着第一个列表是选定列表,第二个列表表示第一个列表的索引。然而,这里的情况似乎并非如此

def flatten(lists):
    results = []
    for outer_index in range(len(lists)): # outer index = 0, 1
        for inner_index in range(len(lists[outer_index])): # inner_index = [0, 1, 2, 0, 1, 2, 3, 4, 5]
            results.append(lists[outer_index][inner_index])
    return results
n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
print(flatten(n))

您正在创建一个列表列表(基本上是一个表)

如果我做了
n[0][1]
我是说转到
第0行
并抓住
第1列中的元素

这样想比较好

n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
s = n[0] # Now s = [1,2,3], the first element in n
s[1] = 2 # Because I just grabbed the second element in [1,2,3]

# This is the same as

n[0][1]

这不是数组的数组,而是列表的列表。@DYZ Oops这是我的
C++
n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
s = n[0] # Now s = [1,2,3], the first element in n
s[1] = 2 # Because I just grabbed the second element in [1,2,3]

# This is the same as

n[0][1]