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.
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.
573 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
918 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
613 GDB gets version error when attempting to debug with the Presense SDK (IDE)
620 Uncaught (inpromise) Error on Electron: An object could not be cloned
574 Who developed the "avformat-59.dll" that comes with FFmpeg?
© 2024 OneMinuteCode. All rights reserved.