Home > Software design >  Segregation based on the list
Segregation based on the list

Time:12-04

I have a list:

list = ['aaazzz0', 'aaazzz1', 'aaazzz2', 'aaazzz3', 'aaazzz4', 'aaazzz5', 'bbbzzz0', 'bbbzzz0', 'bbbzzz1','bbbzzz2','bbbzzz3','bbbzzz4','bbbxxx0','bbbxxx1','bbbxxx2','bbbxxx3']

my desired state is:

`

{
  "aaa": {
    "zzz": [
      "aaazzz0",
      "aaazzz1",
      "aaazzz2",
      "aaazzz3",
      "aaazzz4",
      "aaazzz5"
    ]
  },
  "bbb": {
    "zzz": [
      "bbbzzz0",
      "bbbzzz1",
      "bbbzzz2",
      "bbbzzz3",
      "bbbzzz4"
    ],
    "xxx": [
      "bbbxxx0",
      "bbbxxx1",
      "bbbxxx2",
      "bbbxxx3"
    ]
  }
}

`

my code is:

`

import re, json

list = ['aaazzz0', 'aaazzz1', 'aaazzz2', 'aaazzz3', 'aaazzz4', 'aaazzz5', 'bbbzzz0', 'bbbzzz0', 'bbbzzz1','bbbzzz2','bbbzzz3','bbbzzz4','bbbxxx0','bbbxxx1','bbbxxx2','bbbxxx3']

regex = '^.{3}([a-z]{3})'

all_dict = {}
a = {}
a_list = []
b = {}
b_list = []

for item in list:
    end_match = re.findall(regex, item)[0]
    aaa_match = re.search('aaa', item)
    bbb_match = re.search('bbb', item)
    for suffix in end_match:

        if aaa_match:
            a[suffix] = []
            a_list.append(item)
            a[suffix] = a_list

        elif bbb_match:
            b[suffix] = []
            b_list.append(item)
            b[suffix] = b_list



all_dict["aaa"] = a
all_dict["bbb"] = b

print(json.dumps(all_dict,indent=4))
        

`

so zzz, xxx keys I need to generate dynamicaly based on what is in list element, it's always 4,5,6 char.

this code genearetes the following output, which is not what I'm looking for

`

{
    "aaa": {
        "z": [
            "aaazzz0",
            "aaazzz0",
            "aaazzz0",
            "aaazzz1",
            "aaazzz1",
            "aaazzz1",
            "aaazzz2",
            "aaazzz2",
            "aaazzz2",
            "aaazzz3",
            "aaazzz3",
            "aaazzz3",
            "aaazzz4",
            "aaazzz4",
            "aaazzz4",
            "aaazzz5",
            "aaazzz5",
            "aaazzz5"
        ]
    },
    "bbb": {
        "z": [
            "bbbzzz0",
            "bbbzzz0",
            "bbbzzz0",
            "bbbzzz0",
            "bbbzzz0",
            "bbbzzz0",
            "bbbzzz1",
            "bbbzzz1",
            "bbbzzz1",
            "bbbzzz2",
            "bbbzzz2",
            "bbbzzz2",
            "bbbzzz3",
            "bbbzzz3",
            "bbbzzz3",
            "bbbzzz4",
            "bbbzzz4",
            "bbbzzz4",
            "bbbxxx0",
            "bbbxxx0",
            "bbbxxx0",
            "bbbxxx1",
            "bbbxxx1",
            "bbbxxx1",
            "bbbxxx2",
            "bbbxxx2",
            "bbbxxx2",
            "bbbxxx3",
            "bbbxxx3",
            "bbbxxx3"
        ],
        "x": [
            "bbbzzz0",
            "bbbzzz0",
            "bbbzzz0",
            "bbbzzz0",
            "bbbzzz0",
            "bbbzzz0",
            "bbbzzz1",
            "bbbzzz1",
            "bbbzzz1",
            "bbbzzz2",
            "bbbzzz2",
            "bbbzzz2",
            "bbbzzz3",
            "bbbzzz3",
            "bbbzzz3",
            "bbbzzz4",
            "bbbzzz4",
            "bbbzzz4",
            "bbbxxx0",
            "bbbxxx0",
            "bbbxxx0",
            "bbbxxx1",
            "bbbxxx1",
            "bbbxxx1",
            "bbbxxx2",
            "bbbxxx2",
            "bbbxxx2",
            "bbbxxx3",
            "bbbxxx3",
            "bbbxxx3"
        ]
    }
}

`

I stuck... with my code

CodePudding user response:

I think this code will do the trick:

lst = ['aaazzz0', 'aaazzz1', 'aaazzz2', 'aaazzz3', 'aaazzz4', 'aaazzz5', 'bbbzzz0', 'bbbzzz0', 'bbbzzz1', 'bbbzzz2',
       'bbbzzz3', 'bbbzzz4', 'bbbxxx0', 'bbbxxx1', 'bbbxxx2', 'bbbxxx3']

new_dict_result = {"aaa": {},
                   "bbb": {}
                   }

for item in lst:
    first_letter = item[0]
    first_three_letters = first_letter * 3
    last_three_letters = item[3:6]
    new_dict_result[first_three_letters].setdefault(last_three_letters, [])
    if item not in new_dict_result[first_three_letters][last_three_letters]:
        new_dict_result[first_three_letters][last_three_letters].append(item)

print(new_dict_result)

Good luck !

CodePudding user response:

foo = ['aaazzz0', 'aaazzz1', 'aaazzz2', 'aaazzz3', 'aaazzz4', 'aaazzz5', 'bbbzzz0', 'bbbzzz0', 'bbbzzz1','bbbzzz2','bbbzzz3','bbbzzz4','bbbxxx0','bbbxxx1','bbbxxx2','bbbxxx3']
output = {}
for s in foo:
    output.setdefault(s[:3], {}).setdefault(s[3:6], []).append(s)
print(output) # {'aaa': {'zzz': ['aaazzz0', 'aaazzz1', 'aaazzz2', 'aaazzz3', 'aaazzz4', 'aaazzz5']}, 'bbb': {'zzz': ['bbbzzz0', 'bbbzzz0', 'bbbzzz1', 'bbbzzz2', 'bbbzzz3', 'bbbzzz4'], 'xxx': ['bbbxxx0', 'bbbxxx1', 'bbbxxx2', 'bbbxxx3']}}
  • Related