Python:列表理解无法正常工作
Python: List comprehension not working correctly
以下是学习过程的一部分,非常感谢您的帮助!
我在对列表理解进行逆向工程时遇到问题。我有一个输入数据列表:
tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
我想创建一个新的列表系列,如下所示:
['apples', 'Alice', 'dogs']
['oranges', 'Bob', 'cats']
['cherries', 'Carol', 'moose']
['banana', 'David', 'goose']
我可以使用:
i = 0
for li in range(4):
out = [item[i] for item in tableData]
print(out)
i += 1
然而,当我尝试使用时:
i = 0
out = []
for li in range(4):
for item in tableData:
out.append(item[i])
print(out)
i += 1
它会导致错误。
知道为什么吗?我怎样才能让它像前面的例子一样工作?
@Matt B 引用了错误背后的原因。
但是,这里的有效方法可能是:
print(list(zip_longest(tableData[0],tableData[1], tableData[2])))
甚至更好,广义化。 (感谢@Patrick Haugh)
print(list(zip_longest(*tableData)))
输出:
[('apples', 'Alice', 'dogs'), ('oranges', 'Bob', 'cats'), ('cherries', 'Carol', 'moose'), ('banana', 'David', 'goose')]
Note, I used zip_longest
and not zip
to take care of the extra
elements, Incase of data like (Notice the red apples
and Elon
):
tableData = [['red apples', 'apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David', 'Elon'],
['dogs', 'cats', 'moose', 'goose']]
使用 zip
会错过 banana
和 Elon
给出的输出:
[('red apples', 'Alice', 'dogs'), ('apples', 'Bob', 'cats'), ('oranges', 'Carol', 'moose'), ('cherries', 'David', 'goose')]
但是使用 longest_zip
会将缺失值插入为 None
:
[('red apples', 'Alice', 'dogs'), ('apples', 'Bob', 'cats'), ('oranges', 'Carol', 'moose'), ('cherries', 'David', 'goose'), ('banana', 'Elon', None)]
它会抛出错误,因为您进行了 4 次循环 (range(4)
) 以遍历 tableData
,其中只有 3 个元素。
i = 0
out = []
for li in range(3): # <<< Change to 3
for item in tableData:
out.append(item[i])
print(out)
i += 1
最好循环遍历 tableData
的 len
的 range
,这样它会动态运行,您可以更改 [=13= 的长度] 没有它抛出我认为是 IndexError
:
i = 0
out = []
for li in range(len(tableData))
for item in tableData:
out.append(item[i])
print(out)
i += 1
如果你想打印出tableData
里面的列表,那么你可以这样写:
for item in tableData:
print(item)
马特 B 的例子:
i = 0
out = []
for li in range(len(tableData)):
for item in tableData:
out.append(item[i])
print(out)
i += 1
这也会导致错误,因为i
会越界。
您不必初始化 i
,您可以只附加 item
.
out = []
# No nested for loop. this will cause duplicates of the list
for item in tableData:
out.append(item) #<<< No need for item[i]
print(out)
如果您已经有一个列表列表,那么您不需要创建一个新的列表列表。
以下是学习过程的一部分,非常感谢您的帮助!
我在对列表理解进行逆向工程时遇到问题。我有一个输入数据列表:
tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
我想创建一个新的列表系列,如下所示:
['apples', 'Alice', 'dogs']
['oranges', 'Bob', 'cats']
['cherries', 'Carol', 'moose']
['banana', 'David', 'goose']
我可以使用:
i = 0
for li in range(4):
out = [item[i] for item in tableData]
print(out)
i += 1
然而,当我尝试使用时:
i = 0
out = []
for li in range(4):
for item in tableData:
out.append(item[i])
print(out)
i += 1
它会导致错误。
知道为什么吗?我怎样才能让它像前面的例子一样工作?
@Matt B 引用了错误背后的原因。
但是,这里的有效方法可能是:
print(list(zip_longest(tableData[0],tableData[1], tableData[2])))
甚至更好,广义化。 (感谢@Patrick Haugh)
print(list(zip_longest(*tableData)))
输出:
[('apples', 'Alice', 'dogs'), ('oranges', 'Bob', 'cats'), ('cherries', 'Carol', 'moose'), ('banana', 'David', 'goose')]
Note, I used
zip_longest
and notzip
to take care of the extra elements, Incase of data like (Notice thered apples
andElon
):
tableData = [['red apples', 'apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David', 'Elon'],
['dogs', 'cats', 'moose', 'goose']]
使用 zip
会错过 banana
和 Elon
给出的输出:
[('red apples', 'Alice', 'dogs'), ('apples', 'Bob', 'cats'), ('oranges', 'Carol', 'moose'), ('cherries', 'David', 'goose')]
但是使用 longest_zip
会将缺失值插入为 None
:
[('red apples', 'Alice', 'dogs'), ('apples', 'Bob', 'cats'), ('oranges', 'Carol', 'moose'), ('cherries', 'David', 'goose'), ('banana', 'Elon', None)]
它会抛出错误,因为您进行了 4 次循环 (range(4)
) 以遍历 tableData
,其中只有 3 个元素。
i = 0
out = []
for li in range(3): # <<< Change to 3
for item in tableData:
out.append(item[i])
print(out)
i += 1
最好循环遍历 tableData
的 len
的 range
,这样它会动态运行,您可以更改 [=13= 的长度] 没有它抛出我认为是 IndexError
:
i = 0
out = []
for li in range(len(tableData))
for item in tableData:
out.append(item[i])
print(out)
i += 1
如果你想打印出tableData
里面的列表,那么你可以这样写:
for item in tableData:
print(item)
马特 B 的例子:
i = 0
out = []
for li in range(len(tableData)):
for item in tableData:
out.append(item[i])
print(out)
i += 1
这也会导致错误,因为i
会越界。
您不必初始化 i
,您可以只附加 item
.
out = []
# No nested for loop. this will cause duplicates of the list
for item in tableData:
out.append(item) #<<< No need for item[i]
print(out)
如果您已经有一个列表列表,那么您不需要创建一个新的列表列表。