Home > OS >  split dictionary base on conditions in python
split dictionary base on conditions in python

Time:06-27

i have two dictionary , i want split my product dictionary to lists base on the following conditions :

product = {
    'k1': 30.99999999999994,
    'k2': 400.0,
    'k3': 50.0,
    'k4': 400.00000000000006,
    'k5': 400.0,
    'k6': 50.0,
    'k7': 60.0,
    'k8': 300.0,
    'k9': 40.0}

types = {
    'k1': 2,
    'k2': 1,
    'k3': 1,
    'k4': 1,
    'k5': 1,
    'k6': 1,
    'k7': 1,
    'k8': 1,
    'k9': 2}
  • 1 - if a product has value less that 200 should join the other products while new list of products has value less than 600
  • 2 - product with different types can't join

i have written the following code but it has a bug on last index of dictionary and i get false results every time.

k = list(product.keys())

ProductValue = []
ProductName = []
for idx, name in enumerate(k):
    value = product[name]
    if idx == 0:
        temp1 = [value]
        temp2 = [name]
        continue
    if idx == len(k) - 1:
        ProductValue.append(temp1)
        ProductName.append(temp2)
        continue
    if value < 200 or product[k[idx - 1]] < 200:
        sec1 = types[k[idx - 1]]
        sec2 = types[k[idx]]
        if ((sum(temp1)   value) > 600) or (sec1 != sec2):
            ProductValue.append(temp1)
            ProductName.append(temp2)
            temp1 = [value]
            temp2 = [name]
            continue
        else:
            temp1.append(value)
            temp2.append(name)

    else:
        ProductValue.append(temp1)
        ProductName.append(temp2)
        temp1 = [value]
        temp2 = [name]

what i get from this code is :

ProductValue = [[30.99999999999994], [400.0, 50.0], [400.00000000000006], [400.0, 50.0, 60.0], [300.0]]
ProductName = [['k1'], ['k2', 'k3'], ['k4'], ['k5', 'k6', 'k7'], ['k8']]

which is missing the last product that has value of 40. i tried fixing my code but i made it worse

CodePudding user response:

Edited: Please follow the comments added that'd fix the bug and remove the condition for idx==len(k)-1

product = {
    'k1': 30.99999999999994,
    'k2': 400.0,
    'k3': 50.0,
    'k4': 400.00000000000006,
    'k5': 400.0,
    'k6': 50.0,
    'k7': 60.0,
    'k8': 300.0,
    'k9': 40.0}

types = {
    'k1': 2,
    'k2': 1,
    'k3': 1,
    'k4': 1,
    'k5': 1,
    'k6': 1,
    'k7': 1,
    'k8': 1,
    'k9': 2}


k = list(product.keys())

ProductValue = []
ProductName = []
# create empty sublists
temp1 = []
temp2 = []
for idx, name in enumerate(k):
    value = product[name]
    if idx == 0:
        temp1 = [value]
        temp2 = [name]
        continue
    if value < 200 or product[k[idx - 1]] < 200:
        sec1 = types[k[idx - 1]]
        sec2 = types[k[idx]]
        if ((sum(temp1)   value) > 600) or (sec1 != sec2):
            ProductValue.append(temp1)
            ProductName.append(temp2)
            temp1 = [value]
            temp2 = [name]
            continue
        else:
            temp1.append(value)
            temp2.append(name)

    else:
        # append temp1 and temp1 when for condition fails
        ProductValue.append(temp1)
        ProductName.append(temp2)
        temp1 = [value]
        temp2 = [name]
else:
    # append the last element to the output list
    ProductValue.append(temp1)
    ProductName.append(temp2)


print(ProductName)
print(ProductValue)

Output:

[(['k1'], [30.99999999999994]),
 (['k2', 'k3'], [400.0, 50.0]),
 (['k4'], [400.00000000000006]),
 (['k5', 'k6', 'k7'], [400.0, 50.0, 60.0]),
 (['k8'], [300.0]),
 (['k9'], [40.0])]

Input2:

product = {
    'k1': 30.99999999999994,
    'k2': 400.0,
    'k3': 50.0,
    'k4': 400.00000000000006,
    'k5': 400.0,
    'k6': 50.0,
    'k7': 60.0,
    'k8': 300.0,
    'k9': 40.0}

types = {
    'k1': 2,
    'k2': 1,
    'k3': 1,
    'k4': 1,
    'k5': 1,
    'k6': 1,
    'k7': 1,
    'k8': 1,
    'k9': 1}

Output:

[(['k1'], [30.99999999999994]),
 (['k2', 'k3'], [400.0, 50.0]),
 (['k4'], [400.00000000000006]),
 (['k5', 'k6', 'k7'], [400.0, 50.0, 60.0]),
 (['k8', 'k9'], [300.0, 40.0])]
  • Related