Netsed dictionary in Python












-1














Trying to type a nested dictionary, but the results show the nested value with last entry. I guess this is because I'm trying to add the nested key with is the same for all:



for i in range(15, 19, 1):
left_index = counters[i].find(']')
right_index = counters[i].rfind(': ')
key = counters[i][left_index + 1:right_index]
value = counters[i][right_index + 1:].replace('[(','').replace(')]','').replace(') - (',' ').strip().split(' ')
d1['value'] = value[0]
d1['minimum'] = value[1]
d1['maximum'] = value[-1]
print 'key: ',key, 'value: ', d1
d[key] = d1

encoder.FLOAT_REPR = lambda x: format(x, '.5f')
print json.dumps(d, indent=5, sort_keys=True)


And the result is:



key:   Socket/Modem 1/Bytes sent value:  {'minimum': '0', 'maximum': '2482262614', 'value': '2482262614'}
key: Socket/Modem 1/recv value: {'minimum': '0', 'maximum': '19646', 'value': '19646'}
key: Socket/Modem 1/send value: {'minimum': '0', 'maximum': '2078818', 'value': '2078818'}
key: StreamerEngine/Bonding/Priority queue/Packets of '' priority dequeued value: {'minimum': '0', 'maximum': '0', 'value': '0'}


{
" Socket/Modem 1/Bytes sent": {
"maximum": "0",
"minimum": "0",
"value": "0"
},
" Socket/Modem 1/recv": {
"maximum": "0",
"minimum": "0",
"value": "0"
},
" Socket/Modem 1/send": {
"maximum": "0",
"minimum": "0",
"value": "0"
},
" StreamerEngine/Bonding/Priority queue/Packets of '' priority dequeued": {
"maximum": "0",
"minimum": "0",
"value": "0"
}
}


So you can see that the d1 dictionary is typed well but the accumulated dictionary d is not.










share|improve this question
























  • Possible duplicate of Defining a nested dictionary in python
    – Isma
    Nov 21 '18 at 13:43
















-1














Trying to type a nested dictionary, but the results show the nested value with last entry. I guess this is because I'm trying to add the nested key with is the same for all:



for i in range(15, 19, 1):
left_index = counters[i].find(']')
right_index = counters[i].rfind(': ')
key = counters[i][left_index + 1:right_index]
value = counters[i][right_index + 1:].replace('[(','').replace(')]','').replace(') - (',' ').strip().split(' ')
d1['value'] = value[0]
d1['minimum'] = value[1]
d1['maximum'] = value[-1]
print 'key: ',key, 'value: ', d1
d[key] = d1

encoder.FLOAT_REPR = lambda x: format(x, '.5f')
print json.dumps(d, indent=5, sort_keys=True)


And the result is:



key:   Socket/Modem 1/Bytes sent value:  {'minimum': '0', 'maximum': '2482262614', 'value': '2482262614'}
key: Socket/Modem 1/recv value: {'minimum': '0', 'maximum': '19646', 'value': '19646'}
key: Socket/Modem 1/send value: {'minimum': '0', 'maximum': '2078818', 'value': '2078818'}
key: StreamerEngine/Bonding/Priority queue/Packets of '' priority dequeued value: {'minimum': '0', 'maximum': '0', 'value': '0'}


{
" Socket/Modem 1/Bytes sent": {
"maximum": "0",
"minimum": "0",
"value": "0"
},
" Socket/Modem 1/recv": {
"maximum": "0",
"minimum": "0",
"value": "0"
},
" Socket/Modem 1/send": {
"maximum": "0",
"minimum": "0",
"value": "0"
},
" StreamerEngine/Bonding/Priority queue/Packets of '' priority dequeued": {
"maximum": "0",
"minimum": "0",
"value": "0"
}
}


So you can see that the d1 dictionary is typed well but the accumulated dictionary d is not.










share|improve this question
























  • Possible duplicate of Defining a nested dictionary in python
    – Isma
    Nov 21 '18 at 13:43














-1












-1








-1







Trying to type a nested dictionary, but the results show the nested value with last entry. I guess this is because I'm trying to add the nested key with is the same for all:



for i in range(15, 19, 1):
left_index = counters[i].find(']')
right_index = counters[i].rfind(': ')
key = counters[i][left_index + 1:right_index]
value = counters[i][right_index + 1:].replace('[(','').replace(')]','').replace(') - (',' ').strip().split(' ')
d1['value'] = value[0]
d1['minimum'] = value[1]
d1['maximum'] = value[-1]
print 'key: ',key, 'value: ', d1
d[key] = d1

encoder.FLOAT_REPR = lambda x: format(x, '.5f')
print json.dumps(d, indent=5, sort_keys=True)


And the result is:



key:   Socket/Modem 1/Bytes sent value:  {'minimum': '0', 'maximum': '2482262614', 'value': '2482262614'}
key: Socket/Modem 1/recv value: {'minimum': '0', 'maximum': '19646', 'value': '19646'}
key: Socket/Modem 1/send value: {'minimum': '0', 'maximum': '2078818', 'value': '2078818'}
key: StreamerEngine/Bonding/Priority queue/Packets of '' priority dequeued value: {'minimum': '0', 'maximum': '0', 'value': '0'}


{
" Socket/Modem 1/Bytes sent": {
"maximum": "0",
"minimum": "0",
"value": "0"
},
" Socket/Modem 1/recv": {
"maximum": "0",
"minimum": "0",
"value": "0"
},
" Socket/Modem 1/send": {
"maximum": "0",
"minimum": "0",
"value": "0"
},
" StreamerEngine/Bonding/Priority queue/Packets of '' priority dequeued": {
"maximum": "0",
"minimum": "0",
"value": "0"
}
}


So you can see that the d1 dictionary is typed well but the accumulated dictionary d is not.










share|improve this question















Trying to type a nested dictionary, but the results show the nested value with last entry. I guess this is because I'm trying to add the nested key with is the same for all:



for i in range(15, 19, 1):
left_index = counters[i].find(']')
right_index = counters[i].rfind(': ')
key = counters[i][left_index + 1:right_index]
value = counters[i][right_index + 1:].replace('[(','').replace(')]','').replace(') - (',' ').strip().split(' ')
d1['value'] = value[0]
d1['minimum'] = value[1]
d1['maximum'] = value[-1]
print 'key: ',key, 'value: ', d1
d[key] = d1

encoder.FLOAT_REPR = lambda x: format(x, '.5f')
print json.dumps(d, indent=5, sort_keys=True)


And the result is:



key:   Socket/Modem 1/Bytes sent value:  {'minimum': '0', 'maximum': '2482262614', 'value': '2482262614'}
key: Socket/Modem 1/recv value: {'minimum': '0', 'maximum': '19646', 'value': '19646'}
key: Socket/Modem 1/send value: {'minimum': '0', 'maximum': '2078818', 'value': '2078818'}
key: StreamerEngine/Bonding/Priority queue/Packets of '' priority dequeued value: {'minimum': '0', 'maximum': '0', 'value': '0'}


{
" Socket/Modem 1/Bytes sent": {
"maximum": "0",
"minimum": "0",
"value": "0"
},
" Socket/Modem 1/recv": {
"maximum": "0",
"minimum": "0",
"value": "0"
},
" Socket/Modem 1/send": {
"maximum": "0",
"minimum": "0",
"value": "0"
},
" StreamerEngine/Bonding/Priority queue/Packets of '' priority dequeued": {
"maximum": "0",
"minimum": "0",
"value": "0"
}
}


So you can see that the d1 dictionary is typed well but the accumulated dictionary d is not.







python dictionary






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 15:00









John Kugelman

240k51398453




240k51398453










asked Nov 21 '18 at 13:40









Itaybz

133




133












  • Possible duplicate of Defining a nested dictionary in python
    – Isma
    Nov 21 '18 at 13:43


















  • Possible duplicate of Defining a nested dictionary in python
    – Isma
    Nov 21 '18 at 13:43
















Possible duplicate of Defining a nested dictionary in python
– Isma
Nov 21 '18 at 13:43




Possible duplicate of Defining a nested dictionary in python
– Isma
Nov 21 '18 at 13:43












1 Answer
1






active

oldest

votes


















0














d1 is (probably) declared outside of your loop, so you are using the same object again and again, that's why the last assigned values are used in all other dicts.



Instead define d1 inside your loop:



for i in range(15, 19, 1):
left_index = counters[i].find(']')
right_index = counters[i].rfind(': ')
key = counters[i][left_index + 1:right_index]
value = counters[i][right_index + 1:].replace('[(','').replace(')]','').replace(') - (',' ').strip().split(' ')
d1 = {} # <- new dict!
d1['value'] = value[0]
d1['minimum'] = value[1]
d1['maximum'] = value[-1]
print 'key: ',key, 'value: ', d1
d[key] = d1





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53413373%2fnetsed-dictionary-in-python%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    d1 is (probably) declared outside of your loop, so you are using the same object again and again, that's why the last assigned values are used in all other dicts.



    Instead define d1 inside your loop:



    for i in range(15, 19, 1):
    left_index = counters[i].find(']')
    right_index = counters[i].rfind(': ')
    key = counters[i][left_index + 1:right_index]
    value = counters[i][right_index + 1:].replace('[(','').replace(')]','').replace(') - (',' ').strip().split(' ')
    d1 = {} # <- new dict!
    d1['value'] = value[0]
    d1['minimum'] = value[1]
    d1['maximum'] = value[-1]
    print 'key: ',key, 'value: ', d1
    d[key] = d1





    share|improve this answer


























      0














      d1 is (probably) declared outside of your loop, so you are using the same object again and again, that's why the last assigned values are used in all other dicts.



      Instead define d1 inside your loop:



      for i in range(15, 19, 1):
      left_index = counters[i].find(']')
      right_index = counters[i].rfind(': ')
      key = counters[i][left_index + 1:right_index]
      value = counters[i][right_index + 1:].replace('[(','').replace(')]','').replace(') - (',' ').strip().split(' ')
      d1 = {} # <- new dict!
      d1['value'] = value[0]
      d1['minimum'] = value[1]
      d1['maximum'] = value[-1]
      print 'key: ',key, 'value: ', d1
      d[key] = d1





      share|improve this answer
























        0












        0








        0






        d1 is (probably) declared outside of your loop, so you are using the same object again and again, that's why the last assigned values are used in all other dicts.



        Instead define d1 inside your loop:



        for i in range(15, 19, 1):
        left_index = counters[i].find(']')
        right_index = counters[i].rfind(': ')
        key = counters[i][left_index + 1:right_index]
        value = counters[i][right_index + 1:].replace('[(','').replace(')]','').replace(') - (',' ').strip().split(' ')
        d1 = {} # <- new dict!
        d1['value'] = value[0]
        d1['minimum'] = value[1]
        d1['maximum'] = value[-1]
        print 'key: ',key, 'value: ', d1
        d[key] = d1





        share|improve this answer












        d1 is (probably) declared outside of your loop, so you are using the same object again and again, that's why the last assigned values are used in all other dicts.



        Instead define d1 inside your loop:



        for i in range(15, 19, 1):
        left_index = counters[i].find(']')
        right_index = counters[i].rfind(': ')
        key = counters[i][left_index + 1:right_index]
        value = counters[i][right_index + 1:].replace('[(','').replace(')]','').replace(') - (',' ').strip().split(' ')
        d1 = {} # <- new dict!
        d1['value'] = value[0]
        d1['minimum'] = value[1]
        d1['maximum'] = value[-1]
        print 'key: ',key, 'value: ', d1
        d[key] = d1






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 13:45









        Mike Scotty

        5,53751933




        5,53751933






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53413373%2fnetsed-dictionary-in-python%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            Refactoring coordinates for Minecraft Pi buildings written in Python