-1

What I am trying to do is build a dictionary from a database that contains 5 keys. I can't for some reason get this structure to work, as it looks like it rebuilds the array each time. NOTE, The Input is coming from a database of bytes. leveldb Here is the starting code

 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:

this is what I wish would work

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'}}

this is what I'm stuck doing to get all the data

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'}}

how would I build this to my desired result? With the name being the main key and having all the maps with the name in an array. if I build an array all the maps get added to the array and are not separated by name, array.clear does not work because the database is not in order. I have tried to run it through an if statement clearing the array when the name changes. Maybe sorting first.... Any ideas.. ?

Something Like this , but this will not work

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

This works to reorder, How do I define this dynamically ?

        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'"]}}
Dj Martin
  • 57
  • 4
  • 3
    Needs clarification, show some example input and what you want output – Fraser Langton Aug 28 '21 at 03:24
  • 1
    Why do you have all of those `str` calls in there? If those things are strings, they don't need to be converted. – Tim Roberts Aug 28 '21 at 03:35
  • its coming from a byte database. leveldb – Dj Martin Aug 28 '21 at 03:37
  • 2
    I can't understand the question. How **exactly** is the input formatted? "a database of bytes" tells me approximately nothing. What do you mean when you say that the database "contains five keys"? What is the actual code you are using - not just the line you use to create the dict, but enough code to see the overall process and explain what `data` etc. look like? What actually is *wrong with* the actual output vs. desired? Please read https://stackoverflow.com/help/minimal-reproducible-example, and make it possible for other people to see the problem for themselves. – Karl Knechtel Aug 28 '21 at 03:57
  • 2
    Basically: we need to have code that creates a value for `data` that is *identical* to what you get in your actual code, ideally without requiring a third-party library or a database file. You should make sure you know how to inspect values in Python and make sure that you can reproduce them exactly. Based on the output sample, you should also make sure you understand what a `bytes` object is and how it works. – Karl Knechtel Aug 28 '21 at 04:02
  • I can't even begin to guess what you want `[].append(str(X))` to do. In particular, what is `X`? But you should also read https://stackoverflow.com/questions/16641119/why-does-append-always-return-none-in-python . – Karl Knechtel Aug 28 '21 at 04:03
  • for x in currentMaps: dic[currentMaps[x]["name"]]["maps"] = [currentMaps[x]["map"]} – Dj Martin Aug 28 '21 at 05:07

2 Answers2

1

It's hard to understand exactly what your data looks like, but assuming it's something like this

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'}

You should be able to get it into a dictionary formatted as you'd like with a simple comprehension like

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

Output:

{'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'}}
JASON G PETERSON
  • 2,193
  • 1
  • 18
  • 19
0

So for my situation, where it is hard to determine what the names will be, I figured out that this works, It's most likely not the best way but it will work. Can't find and set the names in the loop without resetting the array. So I am forced to do this.

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"])

This works :) Is there a better way?

Dj Martin
  • 57
  • 4