Reputation: 41
I have an output that looks like this:
output=[[[], [], [], [1582, 2912, 3109, 5711], []],
[[], [1182, 1432, 2127, 2274, 2613, 3024, 3703, 4723, 5288, 5438], [], []],
[[], [], [], [], [27, 574, 835, 1221, 1388, 1525, 1842, 2070, 2547, 3578, 3798, 3932, 4066, 4157, 4350, 4567, 4709, 5176, 5564, 5744], [], []],
[[], [], [], [], []],
[[]],
[[], []],
[[], [1182, 1432, 2127, 2274, 2613, 3024, 3703, 4723, 5288, 5438], [], [], [1452, 2120, 5628]],
[[3610], []],
[[], [], [], []],
[[3842], []],
[[1566], [3842], []],
[[5182, 5569], [], []],
[[], [3842], [], [], [1452, 2120, 5628]],
[[], [], []],
[[]],
[[], [377, 576, 2682, 3071, 5760], [900, 1723, 2658, 3076], []],
[[], []],
[[], [], [], [], [1452, 2120, 5628]],
[[], [1182, 1432, 2127, 2274, 2613, 3024, 3703, 4723, 5288, 5438], [], []]]
for each row of the output, I need to find all the possible combinations of distances of the numbers in one list to the numbers in another list of the row. For example, for the row:
[[1566], [3842], []],
I would only need to find the distance (1566-3842), but for the row:
[[], [377, 576, 2682, 3071, 5760], [900, 1723, 2658, 3076], []],
I need to find all possible combinations of distances. Could someone show me a fast way to do this? Thanks SO much in advance.
I was thinking of doing something like this:
>>> dis=[]
>>> for i in range(len(output)):
for j in output[i]:
if any(abs(num-numb) for num in output[i] for numb in output[i+1]):
di=abs(num-numb)
dis.append(di)
Am I on the right track?
Upvotes: 1
Views: 3634
Reputation: 983
Interesting question, and thank you for your code snippet. I would go for a list comprehension, but also discard any empty lists that you don't need:
In psuedocode:
for each line in your output:
remove the blank results
if there are 2 result sets,
then calculate all (x - y) combinations of distances
In Python:
combinations = []
for line in output:
result = [i for i in line if i]
if len(result) > 1:
combinations.append([abs(x - y) for x in result[0] for y in result[1]])
combinations.append()
uses a list comprehension that efficiently (well, as efficiently as Python can) runs the calculation that I think you are after
Upvotes: 1
Reputation: 101072
You're probably looking for itertools.product:
from itertools import product
row = [[], [377, 576, 2682, 3071, 5760], [900, 1723, 2658, 3076], []]
values = [l for l in row if l] # filter empty lists from the row
for line in ['{} to {}:\t{}'.format(a, b, abs(a-b)) for (a, b) in product(*values)]:
print line
Output:
377 to 900: 523
377 to 1723: 1346
377 to 2658: 2281
377 to 3076: 2699
576 to 900: 324
576 to 1723: 1147
576 to 2658: 2082
576 to 3076: 2500
2682 to 900: 1782
2682 to 1723: 959
...
Upvotes: 1
Reputation: 437
It looks like your rows are nested, meaning a row contains some number of sublists, which each contain some number of distance values or none at all. It looks like you want all combinations of distance values for the entire row.
In that case, for any row, you can flatten the list and then use itertools.combinations
.
If by combinations, you mean all possible pairs for all the values in the row, then this means the combination length denoted by r
is 2.
dis = []
for row in output:
flatrow = sum(row, [])
for a, b in itertools.combinations(flatrow, 2):
di = abs(a - b)
if di:
dis.append(di)
Upvotes: 1