python - Python 3 中的嵌套 map

标签 python python-3.x lambda

我想将我的列表 list = ["a , 1 ", "b , 2 "] 转换为嵌套列表 [["a","1"],[ "b","2"]].

以下作品:

f1_a = map(lambda x :[t.strip() for t in x.split(',',1)],list)

但这不适用于 Python 3(它适用于 Python 2.7!):

f1_b = map(lambda x :map(lambda t:t.strip(),x.split(',',1)),list)

这是为什么?

有没有比 f1_4 更简洁的方法来实现我想要的?

最佳答案

Python 3 的 map返回一个 map 对象,您需要将其显式转换为列表:

f1_b = list(map(lambda x: list(map(lambda t: t.strip(), x.split(',', 1))), lst))

虽然在大多数情况下你应该更喜欢列表理解而不是 map 调用:

f1_a = [[t.strip() for t in x.split(',', 1)] for x in lst]

关于python - Python 3 中的嵌套 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30952526/

相关文章:

python - tensorflow 中 `*` 记录在哪里?

python - 属性错误: 'KeyedVectors' object has no attribute 'get_keras_embedding'

c# - 如何在 C# 中正确释放匿名委托(delegate)/闭包?

python - 使用内存缓冲区的 psycopg2 Postgres COPY EXPERT 到 Pandas read_csv 失败并出现 ValueError

Python:HTTP请求自动截断内容字符串

python-3.x - ValueError:未知标签类型:在实现MLPClassifier时

javascript - 我在使用 Node js 和 AWS Lambda 创建 cookie 时遇到困难

Java 8 流。在 lambda 表达式中向客户端方法抛出异常

python - 类继承

python - 将两个 DataFrame 合并为 block