Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude list items that contain values from another list

There are two lists:

List<string> excluded = new List<string>() { ".pdf", ".jpg" }; List<string> dataset = new List<string>() {"valid string", "invalid string.pdf", "invalid string2.jpg","valid string 2.xml" }; 

How can I filter-out values from the "dataset" list which contain any keyword from the "excluded" list?

like image 900
lekso Avatar asked Jun 28 '12 08:06

lekso


People also ask

How do you exclude a value from one list to another in Python?

The remove() method removes the first matching element (which is passed as an argument) from the list. The pop() method removes an element at a given index, and will also return the removed item. You can also use the del keyword in Python to remove an element or slice from a list.

How do you exclude a list from a list in Python?

In Python, use list methods clear() , pop() , and remove() to remove items (elements) from a list. It is also possible to delete items using del statement by specifying a position or range with an index or slice.

What method do you use to remove an item from a list t at a specific index?

RemoveAt (Int32) Method is used to remove the element at the specified index of the List<T>. Properties of List: It is different from the arrays.

How to exclude values in one list from another in Excel?

Exclude values in one list from another with formula. Quickly exclude values in one list from another with Kutools for Excel. Easily exclude values in one list from another in Excel: The Select Same & Different Cells utility of Kutools for Excel can help you quickly selecting all same cells in one list based on values in another column.

How to get the list of items not in another list?

To get the items from list one list (A) that are not in another list (B) you can use the Linq Except method like this: var a = new List<int>() { 1, 2, 3, 4, 5 }; var b = new List<int>() { 2, 4, 9, 16, 25 }; var aNotB = a.Except(b); aNotB.ToList().ForEach(x => Console.WriteLine(x)); Which will produce the following results: 1 3 5

How do I remove a list from a list in Excel?

Select a blank cell which is adjacent to the first cell of the list you want to remove, then enter formula =COUNTIF ($D$2:$D$6,A2) into the Formula Bar, and then press the Enter key.

Why would I want values from one list that aren’t in another?

There are all sorts of reasons you might want the values from one list that aren’t in another, for instance: Say you have a shopping list, but you’ve just been to the store and bought some of the items, you might want to get everything on the original shopping list that you didn’t just purchase:


1 Answers

var results = dataset.Where(i => !excluded.Any(e => i.Contains(e))); 
like image 105
MarcinJuraszek Avatar answered Oct 02 '22 21:10

MarcinJuraszek