从Python中的特定字典键的值中删除重复项

时间:2015-12-07 06:42:44

标签: python dictionary

我需要从dict中的特定键中删除重复值 例如

我有

data = [{'NAME':'John','AGE':23,'NUMBER':345},
        {'NAME':'Michel','AGE':23,'NUMBER':346},
        {'NAME':'RAHUL','AGE':23,'NUMBER':347},
        {'NAME':'Susea','AGE':23,'NUMBER':346},
        {'NAME':'Wincent','AGE':23,'NUMBER':342}]

在上面我需要将'NUMBER'键唯一唯一 注意:{'NUMBER':346}出现2次。 我需要输出

data = [{'NAME':'John','AGE':23,'NUMBER':'345'},
        {'NAME':'Michel','AGE':23,'NUMBER':346},
        {'NAME':'RAHUL','AGE':23,'NUMBER':347},
        {'NAME':'Wincent','AGE':23,'NUMBER':342}]

(即)删除特定密钥副本的任何重复记录

请帮助我

4 个答案:

答案 0 :(得分:1)

这样的事情:

data = [{'NAME':'John','AGE':23,'NUMBER':345},
        {'NAME':'Michel','AGE':23,'NUMBER':346},
        {'NAME':'RAHUL','AGE':23,'NUMBER':347},
        {'NAME':'Susea','AGE':23,'NUMBER':346},
        {'NAME':'Wincent','AGE':23,'NUMBER':342}]

filtered_data = []
seen = set()
for item in data:
    number = item['NUMBER']
    if number not in seen:
        filtered_data.append(item)
        seen.add(number)
# filtered_data is deduped

答案 1 :(得分:0)

这有效:

key = 'NUMBER'
seen = set()
res = []
for entry in data:
    if not entry[key] in seen:
        res.append(entry)
        seen.add(entry[key])

结果:

>>> res
[{'AGE': 23, 'NAME': 'John', 'NUMBER': 345},
 {'AGE': 23, 'NAME': 'Michel', 'NUMBER': 346},
 {'AGE': 23, 'NAME': 'RAHUL', 'NUMBER': 347},
 {'AGE': 23, 'NAME': 'Wincent', 'NUMBER': 342}]

使用set保存已经看到的值。对于大型列表,这比使用val in seen_list之类的列表更有效。

答案 2 :(得分:0)

创建唯一的数字列表。

<强> ALGO

  1. uni_no是唯一的号码列表。
  2. data1过滤进程的最终输出。
  3. dataloop
  4. 点击NUMBER
  5. 检查data1是否存在迭代项。
  6. 如果没有,请添加到uni_no并将号码添加到>>> data = [{'NAME':'John','AGE':23,'NUMBER':345}, ... {'NAME':'Michel','AGE':23,'NUMBER':346}, ... {'NAME':'RAHUL','AGE':23,'NUMBER':347}, ... {'NAME':'Susea','AGE':23,'NUMBER':346}, ... {'NAME':'Wincent','AGE':23,'NUMBER':342}] >>> >>> data [{'AGE': 23, 'NAME': 'John', 'NUMBER': 345}, {'AGE': 23, 'NAME': 'Michel', 'NUMBER': 346}, {'AGE': 23, 'NAME': 'RAHUL', 'NUMBER': 347}, {'AGE': 23, 'NAME': 'Susea', 'NUMBER': 346}, {'AGE': 23, 'NAME': 'Wincent', 'NUMBER': 342}] >>> uni_no = [] >>> data1 = [] >>> for i in data: ... if i["NUMBER"] not in uni_no: ... uni_no.append(i["NUMBER"] ) ... data1.append(i) ... >>> data1 [{'AGE': 23, 'NAME': 'John', 'NUMBER': 345}, {'AGE': 23, 'NAME': 'Michel', 'NUMBER': 346}, {'AGE': 23, 'NAME': 'RAHUL', 'NUMBER': 347}, {'AGE': 23, 'NAME': 'Wincent', 'NUMBER': 342}]
  7. <强>演示

    <meta name="google-signin-client_id" content="xxxxxxxx-xxxxxxxx.apps.googleusercontent.com">
    
    <div id="gConnect" style="display:inline-block !important;">
                                    <div id="signin-button"></div> 
    </div>
    
    //Script
    
     <script src="https://apis.google.com/js/client:platform.js?onload=startApp" async defer></script>
        <script type="text/javascript">
    
                var auth2 = {};
                var helper = (function () {
                    return {
                        /**
                         * Hides the sign in button and starts the post-authorization operations.
                         *
                         * param {Object} authResult An Object which contains the access token and
                         *   other authentication information.
                         */
                        onSignInCallback: function (authResult) {
                            if (authResult.isSignedIn.get()) {
                                helper.profile();
                            } else if (authResult['error'] || authResult.currentUser.get().getAuthResponse() == null) {
                                console.log('There was an error: ' + authResult['error']);
                                $('#gConnect').show();
                            }
                        },
    
                        /**
                         * Gets and renders the currently signed in user's profile data.
                         */
                        profile: function () {
                            gapi.client.plus.people.get({
                                'userId': 'me'
                            }).then(function (res) {
                                var profile = res.result;
                                var EmailId = profile.emails[0].value;
                                var FullName = res.result.name.givenName;
                                var auth2 = gapi.auth2.getAuthInstance();
                                auth2.signOut().then(function () { });
    
                                linkedinData=res;
                                flag="G";
    
                                $.post('@Url.Action("CheckEmailLogin","Home")',{Email:EmailId,type:flag},function(data){
                                    if(data.msg=="Already registred.")
                                    {
                                        window.location.href = '@Url.Action("SocialLogin", "Home")' + '?UserEMailId=' + EmailId + '&FirstName=' + FullName + '&type=G&UserType='+$("#ddlUserType").val()+'';
                                    }
                                    else if(data.msg=="Not register");
                                    {
                                        $("#DvUserType").modal('show');
                                    }
                                    if(data.msg=="Email is exist.")
                                    {
                                        alert("Email is already registered.");
                                    }
                                });
                            }, function (err) {
                                var error = err.result;
                                console.log(erroe);
                                $('#gConnect').show();
                            });
                        }
                    };
                })();
    
                /**
            * Handler for when the sign-in state changes.
            *
            * param {boolean} isSignedIn The new signed in state.
            */
                var updateSignIn = function () {
                    if (auth2.isSignedIn.get()) {
                        helper.onSignInCallback(gapi.auth2.getAuthInstance());
                    } else {
                        helper.onSignInCallback(gapi.auth2.getAuthInstance());
                    }
                }
    
                /**
                 * This method sets up the sign-in listener after the client library loads.
                 */
                function startApp() {
                    gapi.load('auth2', function () {
                        gapi.client.load('plus', 'v1').then(function () {
                            gapi.signin2.render('signin-button', {
                                scope: 'https://www.googleapis.com/auth/plus.login',
                                fetch_basic_profile: true
                            });
                            gapi.auth2.init({
                                fetch_basic_profile: true,
                                scope: 'https://www.googleapis.com/auth/userinfo.email'
                            }).then(
                                  function () {
                                      auth2 = gapi.auth2.getAuthInstance();
                                      auth2.isSignedIn.listen(updateSignIn);
                                      auth2.then(updateSignIn());
                                  });
                        });
                    });
                }
    

答案 3 :(得分:0)

以下是一些步骤: 1.为您的found_numbers创建一个空列表。 2.迭代所有字典条目。 3.对于每个字典,查看数字键的值是否在您的found_numbers列表中。 如果该号码存在,则这是重复的条目。您可以从列表中删除它 否则找不到这个数字,所以这是带有这个数字的字典的第一个实例。我们需要将此数字添加到found_numbers列表中。

注意:这假定先到先得的态度对待重复的条目。