Python - 返回元组列表

nsc*_*060 2 python python-3.x

我正在尝试返回下面的元组列表

def lambda_handler(event, context):

    t1 = [('a', 'string', 'a', 'string'), ('b', 'string', 'b', 'string')]

    print(t1)
    return t1
Run Code Online (Sandbox Code Playgroud)

当我print列出列表或将列表转换为str->时print,我得到以下内容:

[('a', 'string', 'a', 'string'), ('b', 'string', 'b', 'string')]
Run Code Online (Sandbox Code Playgroud)

当我return得到

[
  [
    "a",
    "string",
    "a",
    "string"
  ],
  [
    "b",
    "string",
    "b",
    "string"
  ]
]
Run Code Online (Sandbox Code Playgroud)

我的挑战是我需要提交 AWS Glue 作业服务的元组列表:

ApplyMapping.apply(frame = datasource0, mappings = [("a", "string", "a", "string"), ("b", "string", "b", "string")], transformation_ctx = "applymapping1")
Run Code Online (Sandbox Code Playgroud)

但我传递了 (MY_LIST_PARAM) 这是命令的输出return

ApplyMapping.apply(frame = datasource0, mappings = MY_LIST_PARAM, transformation_ctx = "applymapping1")
Run Code Online (Sandbox Code Playgroud)

但是,我收到以下错误:

TypeError: Mapping must be specified as a tuple. Got [
Run Code Online (Sandbox Code Playgroud)

我无法对这些值进行硬编码,因为它是一个单独的作业,它采用参数列表并将其应用到作业逻辑中。将列表打印为字符串对我没有帮助(尽管它看起来是正确的),因为我需要一个 format 格式的元组列表,[(),()]而不是[[],[]].

我正在使用Python 3.7。也在 AWS Lambda 中执行(但这应该没有什么区别)

Mas*_*inn 5

也在 AWS Lambda 中执行(但这应该没有什么区别)

我希望确实如此。我猜想数据从 lambda 转移到粘合(或其他)作为 JSON[0],JSON 没有元组,因此 python 将元组转换为 JSON 数组,后者在另一侧成为列表。

如果您调用的 API 在给定列表时抛出异常,则在接收方将列表转换回元组。这只是运行列表理解或 的问题list(map(tuple, MY_LIST_PARAM))

[0] 或具有类似功能/限制的其他格式我想不出任何序列化格式可以提供 Python 元组和列表之间的区别,这只是可变性问题(因此并不真正关心序列化格式),它们可能有类似“匿名结构”或“匿名记录”的东西,但 Python 元组很少会被序列化,至少是隐式地序列化