以下是我编写的代码:
def comb(self, rows, cols):
return [s+t for s in a for t in b]
如果rows
和cols
的值为
rows = ['abc','efg']
cols = ['123','456']
预期输出:['abc123','abc456,'efg123','efg456']
程序输出:['a1', 'a2', 'a3', 'b1', 'b2', 'b3', 'c1', 'c2', 'c3']
我是Python编程新手。你能帮我了解发生了什么事吗?我已经修复了输出,但我想了解为什么会发生这种情况
将其更改为:
输出:
要理解您的列表理解正在做什么,您可以这样重写它:
想必那不是你想要的
尝试使用
zip()
函数:实际上,
zip()
函数将rows
中的每个值与cols
中的每个值配对另一方面,
[s+t for s in a for t in b]
是一个嵌套的for
循环,其中a
的迭代嵌套在b
的迭代中相关问题 更多 >
编程相关推荐