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
#2
Show us what you've tried, tell us (clearly) how it is not working, and we'll help you fix it. Please use python tags for your code and output, see the link in my signature below.

I would suggest looking at for loops, the 'in' operator, and the count and remove methods of lists.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
It's hard to understand what you want, I'll try to translate:

=> you want to loop over slice[] and keep track of the index to be able to refer to count[] right?
=> something like that then to remove the list elements?
if |slice[i+1]-slice[i]|<1:
    if count[i+1]>count[i]:
        del count[i]
        del slice[i]
    else:
        del count[i]
        del slice[i]
=> So you are just missing a way to loop over that stuff, very like something like
for i, sl in enumerate(slice)
should do the trick. You'll just need to be careful with your indices as in the for loop i will start at 0 even if you specify enumerate(slice[1:-1]) account for this in the rest of your logic.

I suppose that's the ugly way to do it, but I'm not good enough to find the pretty one :D
Reply
#4
Ok, I know this code is completely wrong, but this is what I'm trying to achieve...

i=1
j=1
for i in slice1:
    for j in count:
        if slice1[i]==slice1[i+1] and count[j]>count[j+1]:
            del count[j+1]
            del slice1[i+1]
        elif slice1[i]==slice1[i+1] and count[j]<count[j+1]:
            del count[j]
            del slice1[i]
        if slice1[i]==slice1[i-1] and count[j]>count[j-1]:
            del count[j-1]
            del slice1[i-1]
        elif slice1[i]==slice1[i-1] and count[j]<count[j-1]:
            del count[j]
            del slice1[i]
        i=i+1
        j=j+1
Firstly, I'm getting the error "TypeError: list indices must be integers or slices, not float", referring to:

if slice1[i]==slice1[i+1] and count[j]>count[j+1]: 
Secondly, I think that I'm referring to the positions of the element (i.e. the next element up or down from the one I'm looking at) instead of the numerical value of said element - but I have no idea how to fix that.

I want to see if there is an element with a numerical value that is 1 lower or higher than the numerical value of the element that I'm situated on for the list 'slice'. If this is the case, I then want to compare the corresponding 'count' values, and delete the [slice, count] row which has the lowest numerical value for 'count'.

i.e. starting on slice=115, search for slice=114 and slice=116; if either exist, compare count values. According to my list, slice=116 exists, and has count=7; this is lower than the count value of slice=115, which is count=10. Therefore, I would like to delete slice=116 and count=7 from both lists, and move onto the next slice value, which is 130 - and so forth until I have gone through all of the slice values.
Reply
#5
If you want to know how many of something are in a list, you use the count method:

slice = 115
count_under = slice1.count(slice - 1)
count_over = slice1.count(slice + 1)
You are trying to change a list while iterating over the list. That is not going to work. You need to at least make a copy of the list you are iterating over. A shallow copy should suffice here: slice_copy = slice1[:]
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  unable to remove all elements from list based on a condition sg_python 3 371 Jan-27-2024, 04:03 PM
Last Post: deanhystad
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 426 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Delete strings from a list to create a new only number list Dvdscot 8 1,466 May-01-2023, 09:06 PM
Last Post: deanhystad
  restrict user input to numerical values MCL169 2 869 Apr-08-2023, 05:40 PM
Last Post: MCL169
  How to apply function lower() to the list? Toltimtixma 2 756 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 777 Jan-30-2023, 05:16 AM
Last Post: deanhystad
  Checking if a string contains all or any elements of a list k1llcod3 1 1,023 Jan-29-2023, 04:34 AM
Last Post: deanhystad
  Beginner Higher Lower Game wallytan 2 1,537 Sep-29-2022, 05:14 PM
Last Post: deanhystad
  How to change the datatype of list elements? mHosseinDS86 9 1,896 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,538 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