(如何)从字典创建子数组取决于某些键是否存在

时间:2018-07-04 09:19:33

标签: python list dictionary

"dockedItems": [{
        xtype: 'toolbar',
        dock: 'top',
        items: [{
                xtype: 'textfield', //As of Ext JS 5.0 trigger class has been deprecated.  It is recommended to use {@link Ext.form.field.Text Text Field}.
                triggerCls: 'x-form-clear-trigger',
                width: 300,
                onTriggerClick: function () {
                    this.reset();
                    this.focus();
                },
                listeners: {
                    change: function (field, newVal) {
                        var reportBuilderStore = field.up('panel').getStore();
                        if (!Ext.isEmpty(field.value)) {
                            reportBuilderStore.filterBy(function (rec) {
                                var childs = !Ext.isEmpty(rec.get('children')) ? rec.get('children').map(function (x) {
                                        return x.text;
                                    }) : [];
                                var matched = false;
                                for (var val of childs) {
                                    if (val.toUpperCase().match((field.value).toUpperCase())) {
                                        matched = true;
                                        break;
                                    }
                                }
                                if (!Ext.isEmpty(rec.get('text').toUpperCase().match((field.value).toUpperCase())) || rec.get('text').toUpperCase() == "ROOT" || matched)
                                    return true;
                            });
                        } else {
                            reportBuilderStore.clearFilter();
                        }
                    },
                    buffer: 250
                }
            }
        ]
    }
],

这是我现在唯一想到的,如何将它们组合成一行?
例如。我喜欢这种风格: subarray = [] for dic in dics: if "TargetKey" in dic: subarray.append(dic)

尝试避免创建新的数组变量,因为我只需要使用一次。

欣赏任何高级用法,研究python中的更多快捷方式

2 个答案:

答案 0 :(得分:2)

subarray = list(filter(lambda x: 'TargetKey' in x,dics))

答案 1 :(得分:0)

如果您坚持使用一种班轮:

subarray = [dic for dic in dics if "TargetKey" in dic]

您可以内联条件列表中的条件。如果您打算一次使用它并对其进行迭代,请使用生成器:

subarray = (dic for dic in dics if "TargetKey" in dic)