0

I couldn't find an answer for this, I'm a novice programmer so sorry about this dumb question!

Say in python I want to find where the first time the sequence [69, 69] appears in the list [34,34,34,50,39,69,69,54]. Is there a list notation for this? I feel like there is, but I haven't been able to find it. I'm not looking to make a function, I want to learn list/for methods.

so if a = [34,34,34,50,39,69,69,54] how would I find where [69,69] starts? (as a general statement) If I assume that [69,69] is in fact in a?

martineau
  • 119,623
  • 25
  • 170
  • 301
  • You might want to have a [sliding window](https://stackoverflow.com/a/6822773/6779307) of width 2 and then check if that is equal to your target list – Patrick Haugh Nov 17 '17 at 04:25
  • If you don't care about performance: `int(",".join(map(str, [34,34,34,50,39,69,69,54])).index("69,69") / 3)` – Nir Alfasi Nov 17 '17 at 04:27
  • If you only need to deal numbers in the range 0-255, here's a cute shortcut that should be super-fast: `bytes(L).index(bytes(s))` – wim Nov 17 '17 at 04:33
  • Don't believe this is an exact duplicate of an existing question - this question is simpler than the one suggested. Here's my solution: `list(zip(x, x[1:])).index((69,69))`. If a is `[69,69]` then this would work: `list(zip(x, x[1:])).index(tuple(a))` – JGC Nov 17 '17 at 04:41
  • By means of explanation for my answer above: The solution zips the list together with the same list minus the first element. This produces a list with pairs containing (i and i+1) positions. Convert this to a list, then search for the pair in question `(69,69)` – JGC Nov 17 '17 at 04:49

1 Answers1

-1
[x for x in enumerate(a) if x[1] == 69]
Nish
  • 189
  • 2
  • 6
  • I don't believe this answers the OP's question. The OP asked how to find **where** the 69,69 sequence started. – JGC Nov 17 '17 at 04:46