Posts

Showing posts with the label nested-lists

algorithm, closest point between list elements

algorithm, closest point between list elements I have n ordered lists of unequal size (where I do not know beforehand how many lists there are going to be). I need to find the minimum average distance between one element in each list. For example given n=3 for three lists: a = [14, 22, 36, 48] b = [14, 23, 30, 72] c = [1, 18, 24] The output should be (22,23,24) because: mean(abs(22-23), abs(23-24), abs(22-24)) = 1.33333 which is the smallest among all the points in the example above. I tried to implement it in Python as following def alligner(aoa): ''' read arrays of arrays of peaks and return closest peaks ''' #one of arrays is empty if not [y for x in aoa for y in x]: return None # there is the same nr in all array no need to do anything candidate = set.intersection(*map(set, aoa)) if candidate: # returns intersect return [max(list(candidate))] * len(aoa) else: #tried cartesian product via bumpy malloc err pass My doubt is now regarding the...

What is the quickest way in Python to identify lists with common elements A & B in indices [i] & [j] without using expensive for loops?

What is the quickest way in Python to identify lists with common elements A & B in indices [i] & [j] without using expensive for loops? Context: I went through many similar questions on the internet for list filters and tried all suggested solutions. But they all seem to be fine for comparing two different lists. But they do not give desired results for a my list-of-lists. Problem: I have a list of lists and two different numbers that I want to check if they are located in the first two index elements (index[0] and index[1]) of the nested lists within the list of lists. I wish to identify all such lists and then if such lists exist, compare the fourth index member of all those similar lists against a fixed number. My Sample list: [[1, 4, 65, 77, 22.0], [3, 2, 12, 55, 77.0], [1, 4, 16, 99, 13.0]] Numbers to check: `index[0] == 1 and index[1] == 4.` The above list of lists has two such nested lists where the first index member is 1 and second index member is 4. Hence we we now co...