Home > Software engineering >  I need to split array to sub arrays of similar values in python
I need to split array to sub arrays of similar values in python

Time:05-15

lets suppose i have this queryset_array:

queryset_array = [{"category":"a"},{"category":"a"},{"category:"b"},{"category":"b"},{"category":"c"},{"category":"c"}]

How can I convert this array using most efficient pythonic way into:

array_1 = [{"category":"a"},{"category":a"}}]
array_2 = [{"category":"b"},{"category":"b"}]
array_3 = [{"category":"c"},{"category":"c"}]

CodePudding user response:

Try this:

from itertools import groupby

queryset_array = [{"category": "b"}, {"category": "a"}, {"category": "b"}, {"category": "b"}, {"category": "c"},
                  {"category": "d"}]

def key_func(k):
    return k['category']

sorted_data = sorted(queryset_array, key=key_func)
count = 1
for key, value in groupby(sorted_data , key_func):
    print(f'array_{count}= {list(value)}')
    count  = 1

Output

array_1= [{'category': 'a'}]
array_2= [{'category': 'b'}, {'category': 'b'}, {'category': 'b'}]
array_3= [{'category': 'c'}]
array_4= [{'category': 'd'}]

CodePudding user response:

First of all, your dict is invalid I guess you forgot a quotation mark at {"category: "b"}, which would look like this:

[{"category":"a"},{"category":"a"},{"category":"b"},{"category":"b"},{"category":"c"}, {"category":"c"}]

Second, to do exactly what you're looking for, you can do this:

array_1 = (queryset_array[1], queryset_array[2])
array_2 = (queryset_array[3], queryset_array[4])
array_3 = (queryset_array[5], queryset_array[6])

but this will only work if your queryset_array contains exactly 6 values.

If you want to make your code a bit more generic, you can use this:

queryset_array  = [{"category":"a"},{"category":"a"},{"category":"b"},{"category":"b"},{"category":"c"}, {"category":"c"}]

queryset_array2 = []

for x in range(len(queryset_array)):
    if x % 2 == 0:
        queryset_array2.append([queryset_array[x], queryset_array[x 1]])

Or if the input data is not organized like that:

queryset_array = [{"category": "b"},{"category": "a"},{"category": "b"},{"category": "b"},{"category": "c"},{"category": "d"}]

queryset_array2 = {}

for item in queryset_array:

    if item["category"] not in queryset_array2.keys():
        queryset_array2[item["category"]] = []

    queryset_array2[item["category"]].append(item)

CodePudding user response:

See the following another reply without using any library. I have changed the queryset_array to see if it works fine. I have also created a dictionary desired_dictionary which consists of array_x keys.

queryset_array = [{"category":"a"}, {"category":"a"},{"category":"a"}, {"category":"a"},{"category":"b"},{"category":"b"},{"category":"c"},{"category":"a"}, {"category":"b"}, {"category":"c"}, {"category":"c"},{"category":"b"},{"category":"c"}, {"category":"d"}]

def my_fnc(k):
    return k['category']

sorted_queryset_array = sorted(queryset_array, key=my_fnc)
j = 1

values_dictionary = {}
desired_dictionary = {}

for i in sorted_queryset_array: # creating values_dictionary which includes values and how many times encountered. 
    for k,v in i.items():
        val = f"value_{v}"
        if val in values_dictionary:
            j = j   1
            values_dictionary[val] = j
        if val not in values_dictionary:
            values_dictionary[val] = 1
            j = 1

w = 1

desired_list = []

for k,v in values_dictionary.items():
    values = k.split("_")
    variable = f"array_{w}"
    for p in range(0,v):
        desired_list.append({"category": values[1]}) # This list desired_list is the desired data. 
    desired_dictionary[variable] = desired_list # Creating a dict with array_x values. 
    desired_list = []
    w = w   1

print(desired_dictionary)

# for k,v in desired_dictionary.items():
#     print(v)

See the following output:

enter image description here

  • Related