I want to remove consecutive values from the Python list value and print them out.

Asked 2 years ago, Updated 2 years ago, 19 views

# Received data
# Delete input data if more than 5 data are the same number
# Return remaining data
number = int (input ("Please enter the number of questions!"))

def get_data():
    list = []
    result_list = []

    for i in range(number):
        data = int (input (enter answer f"{i+1}!"))
        list.append(data)

    if(list[0:5]==[1,1,1,1,1]):
        del list[0:5]
        result_list = list
        print(result_list)
    else:
        print(list)


get_data()
print(get_data)

Once I wrote it, I set a range and implemented it so that it can be deleted if a continuous value comes out in that range.

For example, I want to delete [2,5,4,3,2,1,1,1,1,1,2,3,5,4,7,5,6] in the list value.

python

2022-09-20 10:24

1 Answers

I'm not sure, so I approached it a little ignorant.

dabandl = [2,5,4,3,2,1,1,1,1,1,2,3,5,4,7,5,6]

for beonho, daban in enumerate(dabandl):
    if beonho >= 4 :
        daban1 = dabandl[beonho - 1]
        daban2 = dabandl[beonho - 2]
        daban3 = dabandl[beonho - 3]
        daban4 = dabandl[beonho - 4]
        if daban == daban1 and daban1 == daban2 and daban2 == daban3 and daban3 == daban4 :
            del dabandl [beonho - 4 : beonho + 1] # Need +1 because we need to get rid of daban too

print(dabandl)


2022-09-20 10:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.