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
  Extracting Elements From A Website List knight2000 2 206 Jul-20-2021, 10:38 AM
Last Post: knight2000
  Make Groups with the List Elements quest 2 212 Jul-11-2021, 09:58 AM
Last Post: perfringo
  Delete list while iterating shantanu97 1 263 Jun-06-2021, 11:59 AM
Last Post: Yoriz
  I cannot delete and the elements from the list quest 4 605 May-11-2021, 12:01 PM
Last Post: perfringo
  List of lists - merge sublists with common elements medatib531 1 492 May-09-2021, 07:49 AM
Last Post: Gribouillis
  Find Common Elements in 2 list quest 4 600 Apr-14-2021, 03:57 PM
Last Post: quest
  How to plot 3D graph of non numerical value? Gevni 0 508 Mar-05-2021, 02:50 PM
Last Post: Gevni
  Sorting Elements via parameters pointing to those elements. rpalmer 3 627 Feb-10-2021, 04:53 PM
Last Post: rpalmer
  Group List Elements according to the Input with the order of binary combination quest_ 19 1,395 Jan-28-2021, 03:36 AM
Last Post: bowlofred
Question How to print multiple elements from multiple lists in a FOR loop? Gilush 6 643 Dec-02-2020, 07:50 AM
Last Post: Gilush

Forum Jump:

User Panel Messages

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