[Solved] Python – How to replace adjacent list elements with a different value

And yes , you can just pass with if/else and for loops : a = [[0,0,0,0,0], [0,1,1,0,0], [0,1,0,1,0], [0,0,1,0,0], [0,0,0,0,0]] to_replace = 1 replacement = 9 for i in range(len(a)): for x in range(len(a[i])): if a[i][x] == to_replace: for pos_x in range(i-1,i+2): for pos_y in range(x-1,x+2): try: if a[pos_x][pos_y] != to_replace: a[pos_x][pos_y] = replacement except … Read more

[Solved] C# get pair closest to value [closed]

You want something like this: public static Pair FindClosest(IEnumerable<Pair> list, int value) { Pair closest = null; if (list != null && list.Count() > 0) { foreach (var pair in list) { if (value <= pair.Max) { closest = pair; break; } } if (closest == null) { closest = list.Last(); } } return closest; … Read more

[Solved] Compare in Python two lists and create a new one that is combinated [closed]

Try using: d = {i[‘service’]: i[‘price’] for i in a} print([{‘service’: i, ‘price’: d.get(i)} for i in b]) Output: [{‘service’: ‘basketball’, ‘price’: None}, {‘service’: ‘yoga’, ‘price’: 30}, {‘service’: ‘soccer’, ‘price’: None}, {‘service’: ‘golf’, ‘price’: 40}] 0 solved Compare in Python two lists and create a new one that is combinated [closed]

[Solved] Possible to extend two lists at once?

It is not directly possible, because what must be on the left side of an assignment cannot be a function call. It can only be built from simple variables, data members, subscripts and commas, parentheses or square brackets. Best that can be done is to use a comprehension or a map on the right side: … Read more

[Solved] How can i make a set in my list in python?

You should try to do the complete code, but the base code is x = [[‘#’, ‘#’, ‘#’, ‘#’, ‘#’, ‘#’, ‘#’], [‘#’, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘#’], [‘#’, ‘ ‘, ‘$’, ‘+’, ‘$’, ‘ ‘, ‘#’], [‘#’, ‘.’, ‘*’, ‘#’, ‘*’, ‘.’, ‘#’], [‘#’, ‘ ‘, ‘$’, ‘.’, … Read more

[Solved] return the list with strings with single occurrence [closed]

Something like this… List<String> result = Stream.of(“A”, “A”, “BB”, “C”, “BB”, “D”) .collect(Collectors.groupingBy( Function.identity(), Collectors.counting())) .entrySet() .stream() .filter(x -> x.getValue() == 1L) .map(Entry::getKey).collect(Collectors.toList()); System.out.println(result); // [C, D] 2 solved return the list with strings with single occurrence [closed]

[Solved] List with numbers and text

There are many options, I describe some of them for you use Dictionary<int, string>Pros: very fast lookupCons: you can not have two string with same number, you don’t have a List var list2d = new Dictionary<int, string>(); list2d[1] = “hello”; list2d[2] = “world!”; foreach (var item in list2d) { Console.WriteLine(string.Format(“{0}: {1}”, item.Key, item.Value); } use … Read more

[Solved] Getting all ordered sublists of ordered lists

from itertools import combinations, permutations perm=[] index = [9,7,6,5,2,10,8,4,3,1] perm.append(index) M = 3 slicer = [x for x in combinations(range(1, len(index)), M – 1)] slicer = [(0,) + x + (len(index),) for x in slicer] result = [tuple(p[s[i]:s[i + 1]] for i in range(len(s) – 1)) for s in slicer for p in perm] 1 … Read more

[Solved] Convert flat list to dictionary with keys at regular intervals [closed]

Here’s a straightforward solution with a simple loop: dict_x = {} for value in list_x: if isinstance(value, str): dict_x[value] = current_list = [] else: current_list.append(value) Basically, if the value is a string then a new empty list is added to the dict, and if it’s a list, it’s appended to the previous list. solved Convert … Read more

[Solved] Find same elements in a list, and then change these elemens to tuple (f.e. 2 to (2,1))

To just add a count you could keep a set of itertools.count objects in a defaultdict: from itertools import count from collections import defaultdict counters = defaultdict(lambda: count(1)) result = [(n, next(counters[n])) for n in inputlist] but this would add counts to all elements in your list: >>> from itertools import count >>> from collections … Read more

[Solved] How can i fix concatenate tuple (not “list”) to tuple

You forgot to call the conversion function for some parameter in your function. I tested and corrected your code, call it this way: is_verified = verified( convert(data[‘k_signer’]), data[‘d_pk’], convert(data[‘d_signer’]), data[‘sig’], convert(data[‘addr’]), convert(data[‘seed’]), convert(data[‘data’]) ) solved How can i fix concatenate tuple (not “list”) to tuple