0

Given a list of integers, for example:

lst = [3,3,6,5,8,3,4,5]

I am then using list comprehension to find out at which index the number 3 appears in this list:

[i for i, x in enumerate(lst) if x == 3]

But now I cannot work out how to see if the number 3 is sitting next to another 3 and return this as True

Sociopath
  • 13,068
  • 19
  • 47
  • 75
Joe Godwin
  • 33
  • 1
  • 6
  • what is your expected output? – Sociopath Mar 09 '20 at 07:01
  • divide each integer by 3, then sum consecutive results to see if the total is 2... – Solar Mike Mar 09 '20 at 07:04
  • 3
    Does this answer your question? [Identify if list has consecutive elements that are equal in python](https://stackoverflow.com/questions/38708692/identify-if-list-has-consecutive-elements-that-are-equal-in-python) – anuragal Mar 09 '20 at 07:06
  • @SolarMike that would potentially falsely match stuff like 4 and 2, not a robust solution for different numbers. To OP: you need to iterate and check current and next number at once, and you dont have to do it in a list comprehension necessarily. See if you can work it out in a simple loop first. – Paritosh Singh Mar 09 '20 at 07:07
  • @ParitoshSingh 2 /3 is not equal to 1, neither is 4 / 3 equal to 1.... unless you are applying rounding... – Solar Mike Mar 09 '20 at 07:15
  • i had a feeling you were relying on that :P, which seems like a bad idea to me. But just to save myself from that kind of blame, i snuck in the part that its "not a robust solution for different numbers". Say, if i wanted to check if there's two consequetive 1s for example, or 0s! (oh boy). I could probably construct other setups that fail. In such a case, i don't like that solution for anyone @SolarMike – Paritosh Singh Mar 09 '20 at 07:22
  • @anuragal yes that does look like it has the answer, thank you very much – Joe Godwin Mar 09 '20 at 07:37

3 Answers3

3

You can use zip() to loop over the data pairwise:

any(a == b == 3 for a, b in zip(lst, lst[1:]))

The chained comparison then to checks to see if both a and b are equal to 3. The function any() checks to see if any of those changed comparisons are true.

FWIW, another way to loop pairwise() is shown in the itertools recipes section of the docs.

Hope this helps :-)

Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
0

But now I cannot work out how to see if the number 3 is sitting next to another 3 and return this as True

Well since your result is a list of all indices where a 3 can be found, you can just check any two consecutive indices and see if they differ by just 1.

Sadly "windowing" iterators still are not part of the standard library, but replicating them is easy enough:

indices = [3,3,6,5,8,3,4,5]
for i, j in zip(indices, indices[1:]):
    ...
Masklinn
  • 34,759
  • 3
  • 38
  • 57
0

Here is another pretty straight forward way to do it (kinda over-commplicated)

def check(index, lst):
    if index > 0 and lst[index] == lst[index - 1] and lst[index] == 3:
        return True
    if index < len(lst) and lst[index] == lst[index + 1] and lst[index] == 3:
        return True
    return False

lst = [3,3,6,5,8,3,4,5]
for index in range(len(lst)):
    print(check(index, lst))

Output:

True
True
False
False
False
False
False
ExplodingGayFish
  • 2,807
  • 1
  • 5
  • 14