python - 为什么字符串在python中被分割成字符
问题描述
以下是我写的代码:
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 编程的新手。你能帮我理解发生了什么吗?我已经修复了输出,但我想了解为什么会这样。
解决方案
要了解您的列表理解在做什么,您可以像这样重写它:
results = []
for s in a:
for t in b:
results.append(s+t)
想必这不是你想要的。
推荐阅读
- c# - 无法在 Visual Studio 2022 上热重载
- c# - C#多变量排行榜
- javascript - Visual Studio Code 根据文件名建议导入路径
- c - 是否有用于 C 的 microtime.h 库
- java - 不确定错误,当我按下按钮 E/AndroidRuntime: FATAL EXCEPTION: main 时导致我的应用程序崩溃的问题
- python - 在没有列表的python中合并两个数字
- reactjs - React 测试库:fireEvent 更改不起作用
- c - 在 C 中,'int *' 是派生数据类型吗?
- java - Swing JFileChooser 只接受 ZIP 和目录
- reactjs - 以这种方式定义反应组件的原因是什么?