【问题标题】:Python dictionary and arrays , howto get the arrays into the dictionary? [closed]Python字典和数组,如何将数组放入字典? [关闭]
【发布时间】:2021-10-27 20:20:38
【问题描述】:

我想要做的是从包含 5 个键的数据库中构建一个字典。由于某种原因,我不能让这个结构工作,因为它看起来每次都重建数组。 注意,输入来自字节数据库。级别数据库 这是起始代码

 for X in world:
            if b'\xff' not in X:
                if b'\x00' not in X:
                    if b'map_' in X:
                        mapd = self.world.level_wrapper._level_manager._db.get(X)
                        data = amulet_nbt.load(mapd, little_endian=True)
                        if data.get("name") != None:

这是我希望的工作

currentMaps[str(data.get("name"))] = {
                               "name": str(data.get("name")),
                               "map": [].append(str(X)),
                               "cols": str(data.get("col")),
                               "rows": str(data.get("row"))
                           }
OutPut: {'cmap_1': {'name': 'cmap_1', 'map': None, 'cols': '15', 'rows': '10'}, 'cmap_151': {'name': 'cmap_151', 'map': None, 'cols': '3', 'rows': '3'}}

这是我为了获取所有数据而一直在做的事情

currentMaps[str(X)] =  {
                                "name":str(data.get("name")),
                                "map": str(X),
                                "cols": str(data.get("col")),
                                "rows": str(data.get("row"))
                            }
OutPut {"b'map_0'": {'name': 'cmap_1', 'map': "b'map_0'", 'cols': '15', 'rows': '10'}, "b'map_1'": {'name': 'cmap_1', 'map': "b'map_1'", 'cols': '15', 'rows': '10'}, "b'map_10'": {'name': 'cmap_1', 'map': "b'map_10'", 'cols': '15', 'rows': '10'}, ........ETC
Desired Output: {'cmap_1': {'name': 'cmap_1', 'map': [map_1, etc], 'cols': '15', 'rows': '10'}, 'cmap_151': {'name': 'cmap_151', [map_157, etc]: None, 'cols': '3', 'rows': '3'}}

我将如何将其构建为我想要的结果?名称是主键,并且所有带有该名称的地图都在一个数组中。如果我构建一个数组,所有地图都被添加到数组中并且没有按名称分隔,array.clear 不起作用,因为数据库不按顺序排列。 我试图通过 if 语句在名称更改时清除数组来运行它。也许先排序...... 有什么想法吗?

类似这样的东西,但这不起作用

dic = {}
for x in currentMaps:
dic[currentMaps[x]["name"]]["maps"] = currentMaps[x]["map"]

这可以重新排序,我如何动态定义它?

        dic = {"cmap_1":{
            "maps":[],
        },
        "cmap_2":{
            "maps":[],
        }}
        for x in currentMaps:
            dic[currentMaps[x]["name"]]["maps"].append(currentMaps[x]["map"])
{'cmap_1': {'maps': ["b'map_0'", "b'map_1'", "b'map_10'", "b'map_100'", "b'map_101'", "b'map_102'", "b'map_103'", "b'map_104'", "b'map_105'", "b'map_106'", "b'map_107'", "b'map_108'", "b'map_109'", "b'map_11'", "b'map_110'", "b'map_111'", "b'map_112'", "b'map_113'", "b'map_114'", "b'map_115'", "b'map_116'", "b'map_117'", "b'map_118'", "b'map_119'", "b'map_12'", "b'map_120'", "b'map_121'", "b'map_122'", "b'map_123'", "b'map_124'", "b'map_125'", "b'map_126'", "b'map_127'", "b'map_128'", "b'map_129'", "b'map_13'", "b'map_130'", "b'map_131'", "b'map_132'", "b'map_133'", "b'map_134'", "b'map_135'", "b'map_136'", "b'map_137'", "b'map_138'", "b'map_139'", "b'map_14'", "b'map_140'", "b'map_141'", "b'map_142'", "b'map_143'", "b'map_144'", "b'map_145'", "b'map_146'", "b'map_147'", "b'map_148'", "b'map_15'", "b'map_16'", "b'map_17'", "b'map_18'", "b'map_19'", "b'map_2'", "b'map_20'", "b'map_21'", "b'map_22'", "b'map_23'", "b'map_24'", "b'map_25'", "b'map_26'", "b'map_27'", "b'map_28'", "b'map_29'", "b'map_3'", "b'map_30'", "b'map_31'", "b'map_32'", "b'map_33'", "b'map_34'", "b'map_35'", "b'map_36'", "b'map_37'", "b'map_38'", "b'map_39'", "b'map_4'", "b'map_40'", "b'map_41'", "b'map_42'", "b'map_43'", "b'map_44'", "b'map_45'", "b'map_46'", "b'map_47'", "b'map_48'", "b'map_49'", "b'map_5'", "b'map_50'", "b'map_51'", "b'map_52'", "b'map_53'", "b'map_54'", "b'map_55'", "b'map_56'", "b'map_57'", "b'map_58'", "b'map_59'", "b'map_6'", "b'map_60'", "b'map_61'", "b'map_62'", "b'map_63'", "b'map_64'", "b'map_65'", "b'map_66'", "b'map_67'", "b'map_68'", "b'map_69'", "b'map_7'", "b'map_70'", "b'map_71'", "b'map_72'", "b'map_73'", "b'map_74'", "b'map_75'", "b'map_76'", "b'map_77'", "b'map_78'", "b'map_79'", "b'map_8'", "b'map_80'", "b'map_81'", "b'map_82'", "b'map_83'", "b'map_84'", "b'map_85'", "b'map_86'", "b'map_87'", "b'map_88'", "b'map_89'", "b'map_9'", "b'map_90'", "b'map_91'", "b'map_92'", "b'map_93'", "b'map_94'", "b'map_95'", "b'map_96'", "b'map_97'", "b'map_98'", "b'map_99'"]}, 'cmap_2': {'maps': ["b'map_149'", "b'map_150'", "b'map_151'", "b'map_152'", "b'map_153'", "b'map_154'", "b'map_155'", "b'map_156'", "b'map_157'"]}}

【问题讨论】:

  • 需要澄清,显示一些示例输入和你想要的输出
  • 为什么你有所有这些str 电话在那里?如果这些东西是字符串,则不需要转换。
  • 它来自字节数据库。级别数据库
  • 我无法理解这个问题。输入的格式究竟如何? “字节数据库”几乎什么也没告诉我。当您说数据库“包含五个键”时,您是什么意思?您使用的实际代码是什么 - 不仅仅是用于创建 dict 的行,还有足够的代码来查看整个过程并解释 data 等的样子?实际输出与期望的错误是什么?请阅读stackoverflow.com/help/minimal-reproducible-example,并让其他人能够自己看到问题。
  • 基本上:我们需要为data 创建值的代码与您在实际代码中得到的值相同,理想情况下不需要第三方库或数据库文件。你应该确保你知道如何在 Python 中检查值,并确保你可以准确地重现它们。根据输出示例,您还应该确保了解 bytes 对象是什么以及它是如何工作的。

标签: python arrays dictionary


【解决方案1】:

因此,对于我的情况,很难确定名称将是什么,我发现这是可行的,这很可能不是最好的方法,但它会起作用。 如果不重置数组,则无法在循环中找到并设置名称。所以我不得不这样做。

dic = {}
        for x in currentMaps:
            dic[currentMaps[x]["name"]] = {}
            dic[currentMaps[x]["name"]]["maps"] = []
            dic[currentMaps[x]["name"]]["col"] = int
            dic[currentMaps[x]["name"]]["row"] = int
        for x in currentMaps:
            dic[currentMaps[x]["name"]]["col"] = currentMaps[x]["cols"]
            dic[currentMaps[x]["name"]]["row"] = currentMaps[x]["rows"]
            dic[currentMaps[x]["name"]]["maps"].append(currentMaps[x]["map"])

这行得通:) 有没有更好的办法?

【讨论】:

    【解决方案2】:

    很难准确理解您的数据是什么样的,但假设它是这样的

    data = [{'name': 'cmap_1', 'map': ['map_1', 'map_2'], 'cols': '15', 'rows': '10'},
    {'name': 'cmap_151', 'map': ['map_157', 'map_158'], 'cols': '3', 'rows': '3'}
    

    您应该能够通过简单的理解将其放入您喜欢的字典中

    print({x['name']:x for x in data})
    

    输出:

    {'cmap_1': {'name': 'cmap_1', 'map': ['map_1', 'map_2'], 'cols': '15', 'rows': '10'}, 'cmap_151': {'name': 'cmap_151', 'map': ['map_157', 'map_158'], 'cols': '3', 'rows': '3'}}
    

    【讨论】:

    • 数据来自 map_x 键,它们每个都包含名称、列和行数据。没有数据库很难做一个有效的例子。
    • 我添加了起始码
    猜你喜欢
    • 2021-08-16
    • 1970-01-01
    • 2021-12-24
    • 2015-04-10
    • 1970-01-01
    • 2017-07-21
    • 2015-10-17
    • 2021-08-07
    • 1970-01-01
    相关资源
    最近更新 更多