Convert String variable to a List [Groovy]
def l = Eval.me(ids) Takes the string of groovy code (in this case “[10,1,9]”) and evaluates it as groovy. This will give you a list of 3 ints.
def l = Eval.me(ids) Takes the string of groovy code (in this case “[10,1,9]”) and evaluates it as groovy. This will give you a list of 3 ints.
Whenever you face a problem like this, try to express the result of the function with the same function. In your case, you can get the result by adding the first number with the result of calling the same function with rest of the elements in the list. For example, listSum([1, 3, 4, 5, 6]) … Read more
List unmodifiableList = Collections.unmodifiableList(list); List newList = new ArrayList(unmodifiableList); Collections.sort(newList); The constructor of ArrayList takes an existing list, reads its elements (without modifying them!), and adds them to the new List.
You can use a set (in CPython since version 2.4) to efficiently look up duplicate values. If you really need an indexed system as well, you can use both a set and list. Doing your lookups using a set will remove the overhead of if Item in List, but not that of List.index(Item) Please note … Read more
.NET has a number of set operations that work on enumerables, so you could take the set intersection to find members in both lists. Use Any() to find out if the resulting sequence has any entries. E.g. if(list1.Intersect(list2).Any())
It sounds like you want something like: // No need to sort sites first var grouped = sites.OrderBy(x => x.Type) .GroupBy(x => x.Type); Then just serialize grouped. However, I don’t know quite what an IGrouping will look like in JSON… and the type will be present in each case. You may want something like: var … Read more
tqdm Using the tqdm package, a fast and versatile progress bar utility pip install tqdm from tqdm import tqdm def process(token): return token[‘text’] l1 = [{‘text’: k} for k in range(5000)] l2 = [process(token) for token in tqdm(l1)] 100%|███████████████████████████████████| 5000/5000 [00:00<00:00, 2326807.94it/s] No requirement 1/ Use a side function def report(index): if index % 1000 … Read more
The extend() method appends to the existing array and returns None. In your case, you are creating an array — [4, 5, 6] — on the fly, extending it and then discarding it. The variable b ends up with the return value of None.
But how can I actually take the content of the IList and add it to my existing ObservableCollection? Do I have to loop over all elements, or is there a better way? While there may be some “better” way which would involve using third party dependencies, some low level manipulation, etc. Simply looping over the … Read more
The Except method returns IEnumerable, you need to convert the result to list: list1 = list1.Except(list2).ToList(); Here’s a complete example: List<String> origItems = new List<String>(); origItems.Add(“abc”); origItems.Add(“def”); origItems.Add(“ghi”); List<String> newItems = new List<String>(); newItems.Add(“abc”); newItems.Add(“def”); newItems.Add(“super”); newItems.Add(“extra”); List<String> itemsOnlyInNew = newItems.Except(origItems).ToList(); foreach (String s in itemsOnlyInNew){ Console.WriteLine(s); } Since the only items which don’t exist … Read more