Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove a List string element if it contains a string element from another List?

Tags:

c#

xml

linq

Setup

I have these three lists.

List<List<string>> tokens = new List<string>();
List<string> token = new List<string>();
List<string> sets = new List<string();

One complete token List that would be in the tokens List.

{"<card>"",
 "    <name>Domri Emblem</name>",
 "    <set picURL="http://magiccards.info/extras/token/Gatecrash/Domri-Rade-Emblem.jpg" picURLHq="" picURLSt="">GTC</set>",
 "    <color></color>",
 "    <manacost></manacost>",
 "    <type>Emblem</type>",
 "    <pt></pt>",
 "    <tablerow>0</tablerow>",
 "    <text></text>",
 "    <token>1</token>",
 "</card>"}

The sets list would look like this.

{"ARB", ..., "AVR", ..., "GTC", ..., "ZEN"}

I want to go through each token in tokens and remove each string in token that contains any of the elements in set.

Example

The tokens list has a few token elements. One token (say token1) has an element like this.

{..., "    <set picURL="http://magiccards.info/extras/token/Gatecrash/Domri-Rade-Emblem.jpg" picURLHq="" picURLSt="">GTC</set>", ...}

Another token (say token2) has these two elements.

{..., "    <set picURL="http://magiccards.info/extras/token/magic-2012/pentavite.jpg" picURLHq="" picURLSt="">M12</set>",
"    <set picURL="http://magiccards.info/extras/token/player-rewards-2004/pentavite.jpg" picURLHq="" picURLSt="">MI</set>", ...}

Say the sets list was modified to contain only {"ARB", "GTC", "M12"}.

How would I go through each token in tokens and remove string elements that contain any of the string elements in sets? So, after that process, token1 will not have that element above and token2 will only have the second presented element?

What I Have Tried

This goes through each token in tokens and removes any element in token that contains the string "GTC".

foreach (var token in tokens)
{
    token.RemoveAll(str => str.Contains("GTC"));
}

I looked through some other questions and found this but it doesn't work.

foreach (var token in tokens)
{
    token.RemoveAll(sets.Contains);
}

Thanks for the help.

like image 904
KrimCard Avatar asked Apr 22 '13 00:04

KrimCard


People also ask

How do you remove elements from one list to another?

Method #3 : Using remove() remove() can also perform this task but only if the exception of not getting specific elements is handled properly. One can iterate for all the elements of the removed list and remove those elements from the original list.

How do you remove a particular item from within a list called mylist?

How do I remove a specific element from a list 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.

How do I remove a specific string from a list?

Method #1 : Using remove() This particular method is quite naive and not recommended to use, but is indeed a method to perform this task. remove() generally removes the first occurrence of K string and we keep iterating this process until no K string is found in list.

How do I remove a string from a list in Python?

Elements from the list (including strings) can be deleted using the remove () function. This code removes the specific single string. The first string that meets the condition is removed. If there are more identical strings, they are going to stay inside the list. You can also remove all matching elements from the list by using the lambda function.

How to remove elements from a list based on the value?

Remove Elements From a List Based on the Values One of the reasons Python is a renowned programming language is the presence of the numerous inbuilt functions. These inbuilt functions are very handy and thereby make Python very convenient to write. Python has an inbuilt function, remove () that helps us to remove elements based on the value.

How to remove a single element from a string in JavaScript?

It’s not, therefore not a single element is removed. In the next check, the element ‘two’ is a string. The condition is met, therefore the function pop () removes this value.

How to remove an element at a particular index in JavaScript?

If you want to remove an element at a particular index only if the value is a string, you can use the pop () function. In the first condition, we check whether the value at index 2 (counting start from 0) is a string. It’s not, therefore not a single element is removed. In the next check, the element ‘two’ is a string.


1 Answers

foreach (var token in tokens)
{
    token.RemoveAll(str => sets.Any(s => str.Contains(s)));
}
like image 55
Julián Urbano Avatar answered Sep 27 '22 20:09

Julián Urbano