Вы можете создать все возможные ключи, которые не являются None
, а затем проверить, какой элемент попадает в какой ключ - с учетом None
с:
data= {'product_id' : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
'product_type': ['shirt', 'shirt', 'shirt', 'shirt', 'shirt', 'hat',
'hat', 'hat', 'hat', 'hat', 'hat', ],
'color' : [None, None, None, 'red', 'blue', None, 'blue',
'blue', 'blue', 'red', 'red', ],
'size' : [None, 's', 'xl', None, None, 's', None, 's', 'xl', None, 'xl', ]}
from itertools import product
# create all keys without None in it
p = product((t for t in set(data['product_type']) if t),
(c for c in set(data['color']) if c),
(s for s in set(data['size']) if s))
# create the things you have in stock
inventar = list( zip(data['product_id'],data['product_type'],data['color'],data['size']))
d = {}
# order things into its categories
for cat in p:
d.setdefault(cat,set()) # uses a set to collect the IDs
for item in inventar:
TY, CO, SI = cat
ID, TYPE, COLOR, SIZE = item
# the (TYPE or TY) will substitute TY for any TYPE that is None etc.
if (TYPE or TY)==TY and (COLOR or CO)==CO and (SIZE or SI)==SI:
d[cat].add(ID)
print(d)
Вывод:
# category-key id's that match
{('shirt', 'blue', 's') : {1, 2, 5},
('shirt', 'blue', 'xl'): {1, 3, 5},
('shirt', 'red', 's') : {1, 2, 4},
('shirt', 'red', 'xl') : {1, 3, 4},
('hat', 'blue', 's') : {8, 6, 7},
('hat', 'blue', 'xl') : {9, 7},
('hat', 'red', 's') : {10, 6},
('hat', 'red', 'xl') : {10, 11}}
[ 1112] Доку:
Я думаю, что нашел решение
Необходимо будет расширить JTree и DefaultTreeSelectionModel.
JTree соответствующие методы:
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
/// Implement selection using "adding" only logic. //
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
@Override
public void setSelectionPath(TreePath path) {
System.out.println("MLDebugJTree: setSelectionPath(" + path + ")");
addSelectionPath(path);
return;
//super.setSelectionPath(path);
}
@Override
public void setSelectionPaths(TreePath[] paths) {
System.out.println("MLDebugJTree: setSelectionPaths(" + paths + ")");
addSelectionPaths(paths);
return;
}
@Override
public void setSelectionRow(int row) {
System.out.println("MLDebugJTree: setSelectionRow(" + row + ")");
addSelectionRow(row);
return;
//super.setSelectionRow(row);
}
@Override
public void setSelectionRows(int[] rows) {
System.out.println("MLDebugJTree: setSelectionRows(" + rows + ")");
addSelectionRows(rows);
return;
//super.setSelectionRows(rows);
}
DefaultSelectionModel соответствующие методы:
package com.ml.tree2.model.impl;
import javax.swing.tree.DefaultTreeSelectionModel;
import javax.swing.tree.TreePath;
public class MLTreeSelectionModel extends DefaultTreeSelectionModel {
private static final long serialVersionUID = -4270031800448415780L;
@Override
public void addSelectionPath(TreePath path) {
// Don't do overriding logic here because addSelectionPaths is ultimately called.
super.addSelectionPath(path);
}
@Override
public void addSelectionPaths(TreePath[] paths) {
if(paths != null) {
for(TreePath path : paths) {
TreePath[] toAdd = new TreePath[1];
toAdd[0] = path;
if (isPathSelected(path)) {
// If path has been previously selected REMOVE THE SELECTION.
super.removeSelectionPaths(toAdd);
} else {
// Else we really want to add the selection...
super.addSelectionPaths(toAdd);
}
}
}
}
HTH.