Home > Mobile >  Is there a way to create a list in python with both continuous and discrete values?
Is there a way to create a list in python with both continuous and discrete values?

Time:08-17

I am looking for a way to create a mixed list in Python like this

List A = [(7-12), (18-23), 45, 67, 43]
List B = [(15-17), 67]

Is there also a function in which I could check if List B fits inside List A? and if it doesn't fit, print out the elements that didn't fit in the list?

Any help would be appreciated. Thanks!

CodePudding user response:

With you can use an IntervalIndex, then compare the pairs:

listA = [(7,12), (18,23), 45, 67, 43]
listB = [(15,17), 67]

A = pd.IntervalIndex.from_tuples([x if isinstance(x, tuple) else (x,x)
                                  for x in listA])
# IntervalIndex([(7, 12], (18, 23], (45, 45], (67, 67], (43, 43]],
#               dtype='interval[int64, right]')

B = pd.IntervalIndex.from_tuples([x if isinstance(x, tuple) else (x,x)
                                  for x in listB])
# IntervalIndex([(15, 17], (67, 67]], dtype='interval[int64, right]')

out = all(any(b<=a for a in A) for b in B)

output: True

CodePudding user response:

It's a bit unclear what you mean by discrete and continuous. Do you want the open interval (7, 12) ?

Regarding the second question. Yes there is :)

I would turn the list into sets and compare them and convert them back into lists if necessary. Se the link for to see the set operations.

https://www.geeksforgeeks.org/python-set-operations-union-intersection-difference-symmetric-difference/

Hope it'll helps

  • Related