Python Forum
How do I loop through a list and delete numerical elements that are 1 lower/higher?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I loop through a list and delete numerical elements that are 1 lower/higher?
#1
I'm trying to go through a list and delete elements that have numerical values that are one value greater/lower than the value that I'm looking at.

My lists are:
slice=[77.0, 115.0, 116.0, 130.0, 140.0, 141.0, 142.0, 144.0, 145.0, 146.0, 151.0, 152.0, 163.0, 166.0, 169.0, 187.0, 188.0, 197.0, 198.0, 199.0, 210.0, 218.0, 219.0, 250.0, 251.0, 256.0, 257.0, 264.0, 279.0, 280.0, 288.0, 291.0, 298.0, 299.0, 329.0, 330.0, 331.0, 336.0, 337.0, 349.0, 358.0, 372.0, 373.0, 374.0, 376.0, 424.0, 425.0]
count=[14, 10, 7, 5, 4, 5, 5, 14, 5, 11, 6, 2, 12, 2, 3, 6, 8, 5, 7, 8, 3, 28, 16, 2, 5, 3, 25, 2, 2, 2, 2, 2, 32, 2, 2, 34, 2, 5, 11, 5, 2, 6, 6, 28, 13, 4, 13]

Basically, I want to start on the second element of the slice list (i.e. i=1, which is 115) and see if i+1 or i-1 exists (in this case, i+1 exists, 116); if so, I then want compare the count values of said slices, and delete the slice and (corresponding count value) that has the lowest count value. After this, I want to move onto the next value on the list (i.e. i=2) and loop this process until I get to the end of the list.

I'm very new to python (my only coding experience prior to this is having done the codecademy python course over 6 months ago), so I'm sorry if these instructions are a bit vague...
Reply


Messages In This Thread
How do I loop through a list and delete numerical elements that are 1 lower/higher? - by neko - Sep-04-2017, 01:16 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  unable to remove all elements from list based on a condition sg_python 3 450 Jan-27-2024, 04:03 PM
Last Post: deanhystad
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 483 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Delete strings from a list to create a new only number list Dvdscot 8 1,547 May-01-2023, 09:06 PM
Last Post: deanhystad
  restrict user input to numerical values MCL169 2 918 Apr-08-2023, 05:40 PM
Last Post: MCL169
  How to apply function lower() to the list? Toltimtixma 2 790 Feb-10-2023, 05:15 PM
Last Post: Toltimtixma
Question Inserting Numerical Value to the Element in Optionlist and Printing it into Entry drbilgehanbakirhan 1 814 Jan-30-2023, 05:16 AM
Last Post: deanhystad
  Checking if a string contains all or any elements of a list k1llcod3 1 1,108 Jan-29-2023, 04:34 AM
Last Post: deanhystad
  Beginner Higher Lower Game wallytan 2 1,601 Sep-29-2022, 05:14 PM
Last Post: deanhystad
  How to change the datatype of list elements? mHosseinDS86 9 2,003 Aug-24-2022, 05:26 PM
Last Post: deanhystad
  Changing a string value to a numerical value using python code and a lamda function Led_Zeppelin 6 1,625 Jul-05-2022, 11:29 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020