【问题标题】:Can I loop an array inside a dictionary definition in python?我可以在python的字典定义中循环一个数组吗?
【发布时间】:2022-01-01 10:51:15
【问题描述】:

我正在尝试将数据推送到 Firebase,我能够遍历一个数组并在每个循环中推送信息。 但我需要在其中添加一些图片(所以这就像在字典定义中循环一个数组)。我有一个数组中的所有链接。 这是我目前的代码。

def image_upload():
for i in range(len(excel.name)):
    doc_ref = db.collection('plans').document('doc-name').collection('collection-name').document()
    doc_id = doc_ref.id
    data = {
        'bedroomLocation': excel.bedroomLocation[i],
        'bedrooms': excel.bedrooms[i],
        'brand': excel.brand[i],
        'catalog': excel.catalog[i],
        'category': excel.category[i],
        'code': excel.code[i],
        'depth': excel.depth[i],
        'description': excel.description[i],
        'fullBaths': excel.fullBaths[i],
        'garage': excel.garage[i],
        'garageBays': excel.garageBays[i],
        'garageLocation': excel.garageLocation[i],
        'garageType': excel.garageType[i],
        'date': firestore.SERVER_TIMESTAMP,
        'id': doc_id,
        'halfBaths': excel.halfBaths[i],
        'laundryLocation': excel.laundryLocation[i],
        'name': excel.name[i],
        'onCreated': excel.onCreated[i],
        'productType': excel.productType[i],
        'region': excel.region[i],
        'sqf': excel.sqf[i],
        'state': excel.state[i],
        'stories': excel.stories[i],
        'tags': [excel.tags[i]],
        'width': excel.width[i],
    }
    doc_ref.set(data)

这很好,但我真的不知道如何遍历链接数组。 这是我在上面复制的块下面尝试的。

for j in range(len(excel.gallery)):
    if len(excel.gallery[j]) != 0:
        for k in range(len(excel.gallery[j])):
            data['gallery'] = firestore.ArrayUnion(
                [{'name': excel.gallery[j][k][0], 'type': excel.gallery[j][k][1],
                  'url': excel.gallery[j][k][2]}])
            print(data)
doc_ref.set(data)

len(excel.gallery)len(excel.name) 的长度相同 但是,每个 j 位置都有不同数量的链接。 如果我在数据定义中声明图库并使用 ArrayUnion 并预定义多个信息,它可以正常工作,但我需要使用该数组将信息推送到 Firebase。

【问题讨论】:

    标签: python arrays firebase google-cloud-firestore


    【解决方案1】:

    当您说“我将所有链接都放在一个数组中”时,我有点困惑。我们能否看到该数组以及您要查找的输出类型?

    还假设 excel.gallery 是一个字典,您可以使用 items() 函数大量清理您的代码。

    for j, k in excel.gallery.items():
        if k:
            data['gallery'] = firestore.ArrayUnion([{'name': k[0], 'type': k[1], 'url': k[2]}])
            print(data)
    
    doc_ref.set(data)
    

    【讨论】:

    • 您好,感谢您的回复。
    【解决方案2】:

    excel.gallery 实际上是一个矩阵,而不是字典。这是此 [[['Images/CFK_0004-Craftmark_Oakmont_Elevation_1.jpeg', 'Elevation', 'url example from firebase'], .... 的示例输出之一,它适用于每个文件。我正在测试 8 张图像和 2 个计划。所以在这种情况下,我的矩阵是 2x4。但是,如果没有匹配,则可能不会有任何文件。我正在寻找的是在数据被推送之前(或在顺序无关紧要之后)添加该计划的所有网址。

    这行得通:

    'gallery': firestore.ArrayUnion(
                [{'name': 'Example Name', 'type': 'Elevation',
                  'url': 'Example url'},
                 {'name': 'Example Name2', 'type': 'First Floor',
                  'url': 'Example url2'}])
    

    但我需要通过 excel.gallery 循环填充该字段

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-10
      • 2017-06-30
      • 1970-01-01
      • 2019-09-17
      • 1970-01-01
      • 2022-06-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多