Can I use the slice method to return a list that excludes ranges in the middle of the original list?

Is there a way to slice through the entire list, excluding a range of values ​​or multiple ranges of values ​​in the middle of the list?
For instance:

 list = [1,2,3,4,5,6,7,8,9,0] print list[......] #some code inside 

I would like the code above to print the list, excluding the range of values, so the output would be the following: [1,2,3,8,9,0] or excluding several ranges of values, so the output would be: [1,2,6,7,0] using slice notation or any other simple method you can offer.

+4
source share
5 answers
 >>> nums = [1,2,3,4,5,6,7,8,9,0] >>> exclude = set(range(4, 8)) >>> [n for n in nums if n not in exclude] [1, 2, 3, 8, 9, 0] 

Another example

 >>> exclude = set(range(4, 8) + [1] + range(0, 2)) >>> [n for n in nums if n not in exclude] [2, 3, 8, 9] 
+2
source

Use list:

 >>> mylist = [1,2,3,4,5,6,7,8,9,0] >>> print [i for i in mylist if i not in xrange(4,8)] [1, 2, 3, 8, 9, 0] 

Or if you want to exclude numbers in two different ranges:

 >>> print [i for i in mylist if i not in xrange(4,8) and i not in xrange(1,3)] [3, 8, 9, 0] 

By the way, it is not a good practice to name a list a list . This is already a built-in function / type.


If the list was unordered and was a list of strings, you can use map() along with sorted() :

 >>> mylist = ["2", "5", "3", "9", "7", "8", "1", "6", "4"] >>> print [i for i in sorted(map(int,mylist)) if i not in xrange(4,8)] [1, 2, 3, 8, 9] 
+3
source

Using the method and exception list

 def method(l, exclude): return [i for i in l if not any(i in x for x in exclude)] r = method(range(100), [range(5,10), range(20,50)]) print r >>> [0, 1, 2, 3, 4, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99] 

My example uses ranges with int. But this method can be any list of elements with any number of exception lists with other elements if the elements have an equal comparison.


Edit: Faster method:

 def method2(l, exclude): ''' l is a list of items, exclude is a list of items, or a list of a list of items exclude the items in exclude from the items in l and return them. ''' if exclude and isinstance(exclude[0], (list, set)): x = set() map(x.add, [i for j in exclude for i in j]) else: x = set(exclude) return [i for i in l if i not in x] 
+1
source

Here's a function that takes several slice objects and provides a list containing only the elements included by these slices. You can exclude items by specifying around what you want to exclude.

 from itertools import chain def sliceAndDice(sequence, *slices): return list(chain(*[sequence[slice] for slice in slices])) 

So, if you have a list of [0,1,2,3,4,5,6,7,8,9] and you want to exclude 4,5,6 in the middle, you can do this:

 sliceAndDice([0,1,2,3,4,5,6,7,8,9], slice(0,4), slice(7,None)) 

This will return [0, 1, 2, 3, 7, 8, 9] .

It works with lists of things that are not numbers: sliceAndDice(['Amy','John','Matt','Joey','Melissa','Steve'], slice(0,2), slice(4,None)) will leave 'Matt' and 'Joey' , the result is ['Amy', 'John', 'Melissa', 'Steve']

This will not work correctly if you go into slices that are out of order or overlap.

It also creates an entire list at once. A better (but more complicated) solution would be to create an iterator class that iterates over only those elements that you want to include. The solution here is sufficient for relatively short lists.

0
source

Given my_list = [1,2,3,4,5,6,7,8,9,0] , on a single line, with enumerate() and range() (or xrange() in Python 2.x):

 [n for i, n in enumerate(my_list) if i not in range(3, 7)] 
0
source

Source: https://habr.com/ru/post/1482803/


All Articles