小编uns*_*age的帖子

排序和比较字典列表 Python

我试图找到一种方法来排序和比较 Python 3.6 中的两个字典列表。我最终只是想list_dict_alist_dict_b有比较==和评价True

下面是数据的样子:

list_dict_a = [
{'expiration_date': None, 'identifier_country': None, 'identifier_number': 'Male', 'identifier_type': 'Gender', 'issue_date': None},
{'expiration_date': None, 'identifier_country': 'VE', 'identifier_number': '1234567', 'identifier_type': 'Foo No.', 'issue_date': None}]

list_dict_b = [
{'identifier_country': 'VE', 'expiration_date': None, 'identifier_type': 'Foo No.', 'issue_date': None, 'identifier_number': '1234567'},
{'identifier_country': None, 'expiration_date': None, 'identifier_type': 'Gender', 'issue_date': None, 'identifier_number': 'Male'}]
Run Code Online (Sandbox Code Playgroud)

数据是相同的,但它的顺序不同(我对初始顺序没有任何控制权)。

当我尝试将它们进行比较时,在执行以下操作时会得到一个错误值: print("does this match anything",list_dict_a == list_dict_b)

这甚至可以做到吗?

python sorting dictionary list python-3.x

6
推荐指数
1
解决办法
2873
查看次数

在Karma中测试Angular $ window.location.href

我试图弄清楚如何正确地将$ window服务注入我的角度控制器,然后进行测试以确保它适当地重定向.目前,我收到一个错误说明undefined is not a constructor (evaluating 'expect(window.location.href).toEqual('/profile')').我的角度控制器的片段如下:

login.submitLogin = function(){
    LoginFactory.loginUser(login.dataset)
        .then(function(response){
            $window.location.href = '/profile'
        },function(response) {
            login.errorMessage = response.data.message;
        });
};
Run Code Online (Sandbox Code Playgroud)

我在Karma的单元测试如下:

describe('Login Controller', function() {

    var controller, window;

    beforeEach(angular.mock.module('app'));

    beforeEach(inject(function(_$controller_, _$window_){
        window = _$window_;
        controller = _$controller_('LoginCtrl',window);
    }));

    describe('Login', function() {

        it('expects controller to be defined', function(){
            expect(controller).to.be.defined;
        });

        it('expects to be redirected after login', function() {
            controller.dataset.username = 'username';
            controller.dataset.password = 'password';
            controller.submitLogin();
            expect(window.location.href).toEqual('/profile');
        });
    });
});
Run Code Online (Sandbox Code Playgroud)

javascript unit-testing mocha.js angularjs karma-runner

4
推荐指数
1
解决办法
6027
查看次数

将字符串转换为字典列表Python 3

我有一条信息如下:

[{"city": "Beverly Hills", "state": "", "postal_code": "", "address": "Some Address", "country": "USA"}, {"city": "New York", "state": "NY", "postal_code": "", "address": "P.O. BOX 52404", "country": "USA"}]
Run Code Online (Sandbox Code Playgroud)

当我这样做type()时显示为<class 'str'>.

如何从字符串中获取此信息到Python 3中的字典列表?

我已经尝试过literal_eval并收到错误malformed node or string:,所以我不确定最好的办法是什么.

编辑

以下是一个应该可重现的示例:

mydata = {'programs': '["France"]', 'ids': '[]', 'citizenships': '[]', 'nationalities': '["FR"]', 'places_of_birth': '[]', 'dates_of_birth': '["1973-03-25"]', 'addresses': '[{"state": null, "postal_code": null, "address": null, "city": null, "country": "FR"}]'}
for key,value in mydata.items():
    if type(value) is str:
        result = literal_eval(value) …
Run Code Online (Sandbox Code Playgroud)

python string dictionary list python-3.x

0
推荐指数
2
解决办法
5343
查看次数