Home > Mobile >  I want to delete values from an interval of keys from a list of key value pair
I want to delete values from an interval of keys from a list of key value pair

Time:10-27

I want to make a function to delete some values from an interval given by two keys from a list of key value pairs in Python. I've tried a lot of things, I already googled it a lot. This is the list of key value pair: [{"day": 1, "sum": 25, "type": in}, {"day": 2, "sum": 55, "type": in}, {"day": 3, "sum": 154, "type": out}, {"day": 4, "sum": 99, "type": in}]. I want to delete the values that have "day" value between 1 and 3. Here's my UI code for this function. I just need to make the delete_transaction_interval(all_transactions, dayStart, dayEnd) working. Could somebody help me please?

    def delete_transaction_interval(all_transactions,dayStart,dayEnd):
    for i in range(0,len(all_transactions)):
        if all_transactions[i]["day"]==dayStart:
            for j in range(i 1, len(all_transactions)):
                if all_transactions[j]["day"]==dayEnd:
                   del all_transactions[i:j]  

def ui_delete_transaction_interval(all_transactions):
         dayStart=int(input("Start day= "))
         dayEnd=int(input("End day= "))
         delete_transaction_interval(all_transactions, dayStart, dayEnd)
         print_all_transactions(all_transactions)

CodePudding user response:

You should not try to modify a list or any other mutable object while enumerating it. Best practice is to create a new object - a list in this case.

Input data adjusted to make it valid Python.

_in = 0
_out = 0

_list = [{"day": 1, "sum": 25, "type": _in}, {"day": 2, "sum": 55, "type": _in}, {"day": 3, "sum": 154, "type": _out}, {"day": 4, "sum": 99, "type": _in}]

new_list = [d for d in _list if not 1 <= d.get('day', 2) <= 3]

print(new_list)

Output:

[{'day': 4, 'sum': 99, 'type': 0}]

CodePudding user response:

You can try the below solution. We create a list comprehension that only selects key/value pairs which satisfy the condition 1 <= "day" <=3:

d1 = [{"day": 1, "sum": 25, "type": "in"}, {"day": 2, "sum": 55, "type": "in"}, {"day": 3, "sum": 154, "type": "out"}, {"day": 4, "sum": 99, "type": "in"}]          
d2 = [i for i in d1 if not 1 <= i.get("day") <=3] 
print(d2)

Output:

[{'day': 4, 'sum': 99, 'type': 'in'}]

CodePudding user response:

do you need something like this?

l = [{"day": 1, "sum": 25, "type": 'in'}, {"day": 2, "sum": 55, "type": 'in'}, {"day": 3, "sum": 154, "type": 'out'}, {"day": 4, "sum": 99, "type": 'in'}]

def delete_transaction_interval(all_transactions,dayStart,dayEnd):
    return [d for e,d in enumerate(all_transactions) if not d.get('day') in range(dayStart,dayEnd 1)]

print(delete_transaction_interval(l,1,3))   #[{'day': 4, 'sum': 99, 'type': 'in'}]

CodePudding user response:

Let me know if this solve you problem :

# Set up a function 

def delete_transaction_interval(all_transactions, dayStart, dayEnd):
    i=0
    while i<=len(all_transactions)-1:
        if all_transactions[i]["day"]>=dayStart and all_transactions[i]["day"]<=dayEnd:
            del all_transactions[i]
        else:
            i =1
    return all_transactions

# Let's try the folowing list

lst=[{"day": 1, "sum": 25, "type": 'in'}, {"day": 2, "sum": 55, "type": 'in'}, {"day": 3, "sum": 154, "type": 'out'}, {"day": 4, "sum": 99, "type": 'in'}]

# Apply the above function

lst=delete_transaction_interval(lst, 1, 3)

lst

Output :

[{"day": 4, "sum": 99, "type": 'in'}]
  • Related