0

I want to iterate over 2 lists, and compare elements from the same list to see if higher or lower. something like

high = [5,7,8,4,2 ... 3] low = [16,4,8,1,48 ... 4]

if number in high > than previous number, add it to the high_list if number in low < previous number, add it to the low_list

output would be

high_list = [5,7,8] low_list = [16,4,1]

def iter_num (high,low):
    some_listH = []
    some_listL = []
    for H,L in zip(high,low):
        x = H +1
        if H > H[x]:
            H = H[x]
            some_listH.append(H)
        if L < L[x]:
            L = L[x]
            some_listL.append(L)
        return some_listH, some_listL
ddema12
  • 11
  • 1
  • Welcome to Stack Overflow! I am not sure what your question is. What do you want to do (that you did mention), what have you tried, what problem are you running into? – Basya Jun 05 '20 at 08:52
  • I can get this to work in C# but not familiar with python and trying to get correct indentation is annoying – ddema12 Jun 05 '20 at 08:53
  • I can fix the indentation but not sure what the code is supposed to do. Perhaps if you also showed the C# code that would help. – DarrylG Jun 05 '20 at 08:55
  • @Basya TypeError: 'float' object is not subscriptable It doesnt like me indexing using x. – ddema12 Jun 05 '20 at 08:57
  • If your work is with two lists separately, comparing elements in a list to elements in the same list, not the other list, it would seem to make more sense to iterate over the lists separately. – Basya Jun 05 '20 at 08:57
  • Thanks for giving the description of the error; it would be even better to edit that into the question. – Basya Jun 05 '20 at 08:58
  • What are you passing in for the parameters? Are they the lists? You mentioned two lists... Are they lists of floating point numbers? – Basya Jun 05 '20 at 09:00
  • Are you basically trying to compare list[i] with list[i+1]? – Basya Jun 05 '20 at 09:07
  • @Basya I think ill re-post my question, but thanks for the help – ddema12 Jun 05 '20 at 09:08
  • you can look at: https://stackoverflow.com/questions/14012562/how-to-compare-two-adjacent-items-in-the-same-list-python – Basya Jun 05 '20 at 09:08
  • @Basya yeah exactly just like insertion sort algorithm – ddema12 Jun 05 '20 at 09:09
  • You don't need to post a separate question; you can edit the existing one. – Basya Jun 05 '20 at 09:11
  • I think the answer in that link could help you. – Basya Jun 05 '20 at 10:19
  • The question is much clearer now. Have you looked at the link I put in a previous comment? – Basya Jun 05 '20 at 10:35
  • @Basya yes, the linked helped, thank you. I just need to workout how to make the example wok for for two lists. thanks – ddema12 Jun 05 '20 at 11:34

1 Answers1

0

You have written:

 H > H[x]

Either H is an element of a list, or it is a list. It is not both.

The two lists essentially have nothing to do with eachother. If they are not guaranteed to be the same length, I would not recommend handling them in the same loop.

This should do what you want for one list. You can figure out from this how you want to handle the second list, put it into a function, etc.

list = [5,7,8,4,2,1,1,1,3]
some_listL = []

some_listL.append(list[0])
for x, y in zip(list, list[1:]):
    if y > x:
        some_listL.append(y)

print (some_listL)

To give credit where credit is due, I learned how to do this from here

Basya
  • 1,477
  • 1
  • 12
  • 22
  • The desired result would be [5,7,8] this returns [5,7,8,3] – ddema12 Jun 05 '20 at 11:38
  • you'll have to tune the comparison. According to the way I understood your description, if a number was bigger than the previous one, it should be added to the new list. 3 is bigger than 1. Adjust the comparison however you need; the building blocks are here. – Basya Jun 05 '20 at 15:46
  • Thanks man, I slept on it, realised I didnt need to iterate x,y in the same list. Rather x = 0 and if x < item in list, x = item in list Really appreciate your help – ddema12 Jun 06 '20 at 03:44
  • A pleasure. BTW, the way it works on this site, you say 'thank you' by upvoting the question. For the original poster (in this case, you), there is also the option of accepting the answer, if it solved your problem, by clicking on the checkmark next to the answer. – Basya Jun 06 '20 at 19:55