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