Bottom Page

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?
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...
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 -
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

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]
        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
Ok, I know this code is completely wrong, but this is what I'm trying to achieve...

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]
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.
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 -
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures


Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  dict elements are sometimes treated as List and sometimes as String phython_mdr 4 124 Apr-01-2020, 12:47 PM
Last Post: phython_mdr
  TypeError indexing a range of elements directly on the list JFerreira 2 100 Mar-30-2020, 04:22 PM
Last Post: bowlofred
  lower() applied at in operator Beerforfree 3 149 Mar-22-2020, 11:51 AM
Last Post: ndc85430
  adding elements to a list that are more than a specific number Olavv 2 122 Mar-19-2020, 06:05 PM
Last Post: Olavv
  How do i multiply elements in a list together Olavv 6 160 Mar-09-2020, 10:06 AM
Last Post: Olavv
  Adding elements to a list by number Olavv 4 186 Mar-08-2020, 11:16 AM
Last Post: ndc85430
  List of n random elements medatib531 5 309 Mar-07-2020, 07:09 PM
Last Post: jefsummers
  Trying to print corresponding elements in two different list Darthmarvolo 5 209 Mar-03-2020, 04:29 AM
Last Post: perfringo
  multiplying elements in a list Olavv 3 201 Feb-27-2020, 04:55 PM
Last Post: DeaD_EyE
  Filtering Excel Document Data Based On Numerical Values eddywinch82 30 2,070 Feb-25-2020, 06:08 PM
Last Post: eddywinch82

Forum Jump:

Users browsing this thread: 1 Guest(s)