Understanding the Implementation of an Algorithm to Apply the Absorption of Logical Operations in Python

Asked 1 years ago, Updated 1 years ago, 77 views

We have rewritten the following algorithms in Python that apply the absorption of logical operations implemented in Java:

 1 Vector matrix = [a], [a, b], [a, c];
2 for (inti=0;i<matrix.size();i++){
3 Vector element_left=matrix.get(i);
4
5 for (int j=i+1;j<matrix.size();j++){
6 Vector element_right = matrix.get(j);
7
8 if (isIncluded (left, right)) {
9// If the left element is a subset of the right element,
10 matrix.remove(j);
11 j--;
12       }
13    }
14 }

I don't know how to implement Python in line 10 and 11, "If the i-th element is the j-th subset, delete the j-th element and decrement j."

Could you tell me how to write this in Python?
I'm sorry to bother you with a basic question, but I appreciate your cooperation.
※ I think there is a way to use Python's Set{}, but if possible, I would appreciate it if you could use a two-dimensional array.

python java algorithm

2022-09-30 21:38

1 Answers

As you pointed out, you can easily determine if one list is a subset of another by using set.issubset().

If you don't want to use it, you should be able to implement it in Python just like Java implemented isIncluded().For example, as defined in the subset, one element is checked step by step to see if it contains all of the other elements (but you must decide whether to ignore duplication of elements).

You can also remove elements from the list by using matrix.remove(matrix[i]) or by using del matrix[i].This time, del matrix[i] seems to be more suitable because it was deleted by index.

Supplemental: But instead of looping and deleting it, I would choose a new list and looping and append to it because it's no longer in-place, but it's hard to get bugs around the index.


2022-09-30 21:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.