Python List Comprehensions 创建权力列表理解编程

Python List Comprehensions Create a List of Powers Understanding the Programming

所以问题来自

https://github.com/Asabeneh/30-Days-Of-Python/blob/master/13_Day_List_comprehension/13_list_comprehension.md

使用列表理解创建以下元组列表:

[(0, 1, 0, 0, 0, 0, 0),
(1, 1, 1, 1, 1, 1, 1),
(2, 1, 2, 4, 8, 16, 32),
(3, 1, 3, 9, 27, 81, 243),
(4, 1, 4, 16, 64, 256, 1024),
(5, 1, 5, 25, 125, 625, 3125),
(6, 1, 6, 36, 216, 1296, 7776),
(7, 1, 7, 49, 343, 2401, 16807),
(8, 1, 8, 64, 512, 4096, 32768),
(9, 1, 9, 81, 729, 6561, 59049),
(10, 1, 10, 100, 1000, 10000, 100000)]

以下是我的解决方案:

powers_list = [tuple([i] + [i**j for j in range(6)]) for i in range(11)]
print(powers_list)

下一部分是我从 GeeksForGeeks 那里得到的:


matrix = []
  
for i in range(11):
    # Append an empty sublist inside the list
    matrix.append([i])

    for j in range(6):
        matrix[i].append(i**j)
print(matrix)

我理解极客中的极客部分。但是,列表理解让我感到困惑。为什么以及如何

[i] + [i**j..]]

[i] append/prepend 到列表而不是导致 [[0], [1, 0, 0, 0, 0]]?

+ 的功能是否与 listA.extend(listB) 相同?

列表属于序列class,序列提供了一种连接方法 类似于您在字符串中的连接。

串联

a=[1,2,3]
b=[2,3,4]
c=a+b
c=[1,2,3,2,3,4]