1

I need to compare two list of vectors and take their equal elements, like:

veclist1 = [(0.453 , 0.232 , 0.870), (0.757 , 0.345 , 0.212), (0.989 , 0.232 , 0.543)]
veclist2 = [(0.464 , 0.578 , 0.870), (0.327 , 0.335 , 0.562), (0.757 , 0.345 , 0.212)]

equalelements = [(0.757 , 0.345 , 0.212)]

obs: The order of the elements don't matter!

And also, if possible I wanted to only consider till the 2nd decimal in the comparison but without rounding or shortening them. Is it possible ? Thx in advance!

ncica
  • 7,015
  • 1
  • 15
  • 37
  • Which vector should be in the result if 2 vectors are considered equal, but are not the same? e.g. `(0.000, 0.200, 0.500)` and `(0.001, 0.199, 0.502)`. – ikkuh Nov 29 '19 at 14:03
  • My problems is that I have to consider "equal" vectors with a certain amount of respite, but, in the end they should be exactly as they were before, because they describe an exact position in space. – Guilherme Schönmann Finardi Nov 29 '19 at 15:13

1 Answers1

1
# Get new lists with rounded values
veclist1_rounded = [tuple(round(val, 2) for val in vec) for vec in veclist1]
veclist2_rounded = [tuple(round(val, 2) for val in vec) for vec in veclist2]

# Convert to sets and calculate intersection (&)
slct_rounded = set(veclist1_rounded) & set(veclist2_rounded)

# Pick original elements from veclist1:
#   - get index of the element from the rounded list
#   - get original element from the original list
equalelements = [veclist1[veclist1_rounded.index(el)] for el in slct_rounded]

In this case we select the entries of veclist1 if only the rounded entries are equal. Otherwise the last line needs to be adjusted.

If all original elements are needed, the final list can be calculated using both original lists:

equalelements = ([veclist1[veclist1_rounded.index(el)] for el in slct_rounded]
                 + [veclist2[veclist2_rounded.index(el)] for el in slct_rounded])

Note: round might have issues, which should be solved in current Python version. Nevertheless, it might be better to use strings instead:

get_rounded = lambda veclist: [tuple(f'{val:.2f}' for val in vec) for vec in veclist]
veclist1_rounded, veclist2_rounded = get_rounded(veclist1), get_rounded(veclist2)
mcsoini
  • 6,280
  • 2
  • 15
  • 38
  • I see! So that 1st code should convert the vectors back after the operation is done? (sorry, I'm bad at coding), – Guilherme Schönmann Finardi Nov 29 '19 at 15:15
  • do you need *all* coordinates if only the rounded coordinates match? (and the original coordinates are slightly different) – mcsoini Nov 29 '19 at 15:41
  • not totally sure, only testing. Because the object will still be at same place, what I need is it's coordinates to apply texture upon it. I'm trying to develop a cel-shading art style based on a lecture of people from Dragon Ball Fighterz. – Guilherme Schönmann Finardi Nov 29 '19 at 17:08