0

I'm setting up a new script where I need to check if a list has all the elements between 2 numbers, to check the continuity of this list.

At the moment, I've tried to add the minimum value and the maximum value, and I compare it with the number of the elements on the list.

if max+min > 2*count:
   continuity = 'no'
else:
   continuity = 'yes'

Update

This is not a duplicate question. I'll explain with further information my question. I want to check If a study is realized on every X-value and Y-value.

For example, with 4 points, I have:

X=[1, 1, 2, 2]
Y=[1, 1, 2, 2]

Referring to X1=(1,1), X2=(1,2), ... and so on.

At the moment, I've tried with 2 dict:

countY = dict([(k, Y.count(k)) for k in set(Y)])
countX = dict([(k, X.count(k)) for k in set(X)])

I compare it with the max and min value, but I just search a function (if it exists) which allows me to do this, using less lines.

Bando
  • 1,223
  • 1
  • 12
  • 31
  • Please define `continuity` with an example or two – Devesh Kumar Singh Jun 18 '19 at 08:48
  • `list = [-2, -1, 0, 1, 2]` with this list: `continuity = 'yes'` but `list = [-2, 0, 2]` is not. – Bando Jun 18 '19 at 08:50
  • is the list sorted? can it contain duplicate elements? – Devesh Kumar Singh Jun 18 '19 at 08:51
  • My list is not sorted and it can contains some duplicate elements. I've tried to use the most popular answer of your link but when I'm using `sorted(l) == list(range(min(l), max(l)+1))`, this error appears: `'int' object is not callable`. – Bando Jun 18 '19 at 09:04
  • 1
    That's because you have overridden the builtin ``min`` and ``max`` functions with your minimum and maximum values. Use a different name for them. Note that the answers to the other question do not properly handle duplicates, though. – MisterMiyagi Jun 18 '19 at 09:05
  • I achieve to get my list sorted but it doesn't return if the list had consecutive numbers. – Bando Jun 18 '19 at 09:16
  • Also see https://stackoverflow.com/q/8664708/4014959 and https://stackoverflow.com/a/44392621/4014959 – PM 2Ring Jun 18 '19 at 09:19
  • How do you want to handle duplicate items? Should `[5, 2, 3, 2, 4, 1]` pass the test? – PM 2Ring Jun 18 '19 at 09:30
  • I have updated my question above. – Bando Jun 18 '19 at 09:30
  • Sorry, with the update, it's now even harder to understand what you're trying to do. But I'll reopen the question. – PM 2Ring Jun 18 '19 at 09:33
  • @PM2Ring I should not pass because each number has not the same number of apparition in the list. I know this not my first issue but I've update it. – Bando Jun 18 '19 at 09:33
  • We need a few clearer examples of input data and expected output. We need examples that pass the "consecutive" test, and examples that fail the test. – PM 2Ring Jun 18 '19 at 09:35
  • @PM2Ring I think you can close it, I think my issue is too precise to get an answer. Thanks for your fast answers btw! – Bando Jun 18 '19 at 09:36
  • You should also fix the code so you are not using the names `min` & `max` as variable names. BTW, it would be simpler to set `continuity` to `False` or `True`, rather than `"no"` or `"yes"` – PM 2Ring Jun 18 '19 at 09:38
  • Yes, I'll follow these advice! – Bando Jun 18 '19 at 09:39
  • I'm happy to keep it open, if you can explain the task more clearly, with examples. – PM 2Ring Jun 18 '19 at 09:39
  • I will! Let me just check another issue with my script and I will add some examples. – Bando Jun 18 '19 at 09:40

1 Answers1

3

You could do this, which handles duplicate elements:

assert len(set(my_list) - set(range(min_, max_))) == 0 

like

my_list = [2, 2, 3, 1, 4]
min_ = 1
max_ = 5
ref = set(range(min_, max_)) # all the elements you want, in a set
assert len(set(my_list) - ref) == 0
# or
assert all(x in ref for x in my_list)
# or 
assert sorted(my_list) == sorted(ref) # fails if there are repeating element in my_list

test:

import random

def f(l, min_, max_):
    assert sorted(l) == list(range(min_, max_))

def g(l, min_, max_):
    s = set(l)
    ref = set(range(min_, max_))
    assert len(s - ref) == 0

def h(l, min_, max_):
    s = set(l)
    ref = set(range(min_, max_))
    assert all(x in ref for x in s)

min_ = -100
max_ = 100
l = list(range(min_, max_))
random.shuffle(l)

%timeit f(l, min_, max_)
#28.2 µs ± 6.1 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
%timeit g(l, min_, max_)
#12.5 µs ± 1.52 µs per loop (mean ± std. dev. of 7 runs, 100000 loops each)
%timeit h(l, min_, max_)
#28.8 µs ± 12 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
ted
  • 13,596
  • 9
  • 65
  • 107