小编Geo*_*kie的帖子

Python将两个列表与所有可能的排列合并

我试图找出将两个列表合并为所有可能组合的最佳方法.所以,如果我从这样的两个列表开始:

list1 = [1, 2]
list2 = [3, 4]
Run Code Online (Sandbox Code Playgroud)

结果列表如下所示:

[[[1,3], [2,4]], [[1,4], [2,3]]]
Run Code Online (Sandbox Code Playgroud)

也就是说,它基本上产生一个列表列表,其中包含两者之间的所有潜在组合.

我一直在研究itertools,我很确定答案,但是我无法想出办法让它以这种方式行事.我最接近的是:

list1 = [1, 2, 3, 4]
list2 = [5, 6, 7, 8]
print list(itertools.product(list1, list2))
Run Code Online (Sandbox Code Playgroud)

哪个产生:

[(1, 5), (1, 6), (1, 7), (1, 8), (2, 5), (2, 6), (2, 7), (2, 8), (3, 5), (3, 6), (3, 7), (3, 8), (4, 5), (4, 6), (4, 7), (4, 8)]
Run Code Online (Sandbox Code Playgroud)

因此,它会在每个列表中执行所有可能的项目组合,但不是所有可能的结果列表.我怎么做到这一点?

编辑:最终目标是能够单独处理每个列表以确定效率(我正在使用的实际数据更复杂).因此,在上面的原始示例中,它将工作如下:

list1 = [1, 2]
list2 = [3, 4]

Get first merged list: [[1,3], [2, …
Run Code Online (Sandbox Code Playgroud)

python list python-itertools

18
推荐指数
4
解决办法
3万
查看次数

jsTree dnd事件未触发

我正在尝试在jsTree 3.0.0中捕获dnd事件.我使用演示代码来构建事件处理程序.树很好,但事件永远不会发生.我错过了什么?

这是相关的部分.JSON工作正常并构建树本身查找.但是,当我在树上拖放时,console.log调用永远不会发生.

<link href="/jquery/jquery-ui-1.10.3/css/ui-lightness/jquery-ui-1.10.3.custom.min.css" rel="Stylesheet"/>
<script src="/jquery/jquery-ui-1.10.3/js/jquery-1.9.1.js"/>
<script src="/jquery/jquery-ui-1.10.3/js/jquery-ui-1.10.3.custom.min.js"/>
<link href="/jquery/plugins/jsTree/themes/default/style.min.css" rel="stylesheet"/>
<script src="/jquery/plugins/jsTree/jstree.js"/>
<script>
    $(function () {
        $('#jstree')
        // listen for events
        .on('dnd_start.vakata', function (e, data) {
            console.log('Dragged');
        })
        .on('dnd_stop.vakata', function (e, data) {
            console.log('Dropped');
        })
        // create the instance
        .jstree({
            "core" : {
                "check_callback" : true,
                'data' : {                              
                    'url' : 'categoriesJson.pm?json=1',
                    'data' : function(node) {
                        console.log (node);
                        return {
                            'id' : node.id
                        }
                    }
                },
                "themes" : {
                    "stripes" : true
                }
            },
            "plugins" : [ …
Run Code Online (Sandbox Code Playgroud)

drag-and-drop jstree

6
推荐指数
2
解决办法
2万
查看次数

标签 统计

drag-and-drop ×1

jstree ×1

list ×1

python ×1

python-itertools ×1