Decrease the length of a list while iterating over its items










1














Problem:
There are 2 lists, one is considered the parent, and the other is the child. What I want to do is to construct a 3rd list based on a condition.



Current Solution:



from netaddr import *

l1 = ['10.0.0.0/8', '172.16.0.0/16']
l2 =['10.10.10.10','172.16.15.0/24','10.20.10.0/24','13.1.1.0/24','15.10.10.0/24','172.16.16.0/25','10.10.11.11']

[ip1 for ip1 in l1 for ip2 in l2 if IPNetwork(ip2) in IPNetwork(ip1)]


Output:



['10.0.0.0/8', '10.0.0.0/8', '10.0.0.0/8', '172.16.0.0/16', '172.16.0.0/16']


The above comprehension, in my opinion, isn't an optimal solution



I am looking to find a way by which I could re-implement the above solution with the added advantage of removing the elements (refer ip2) that are matched from l2 in every iteration, thus avoid rechecking those elements again in the next iteration










share|improve this question























  • Tip for the future: don't use the 'quote' markup to highlgiht text. Don't use capitals and refrain from using too many stylistic changes if the mere reason is to draw attention.
    – Bram Vanroy
    Nov 12 '18 at 11:00










  • Judging by your question and the auxiliary information, you might be trying to solve the wrong problem. Can you explain how supernet, networks and addresses translate to the two lists? Also, do you know how a set and item in container checks work?
    – MisterMiyagi
    Nov 12 '18 at 11:38










  • @MisterMiyagi I'm not sure what you mean by "translate to the two lists" but I have a clear problem; a list which has a CIRD/Supernet which is considered like a top of the tree. On the other side, I have another list with more-specific networks and addresses, and I want to categorize these addresses based upon the supernet
    – Mohamed Kamal
    Nov 12 '18 at 22:26











  • Does the number and order of matches matter?
    – MisterMiyagi
    Nov 14 '18 at 14:28















1














Problem:
There are 2 lists, one is considered the parent, and the other is the child. What I want to do is to construct a 3rd list based on a condition.



Current Solution:



from netaddr import *

l1 = ['10.0.0.0/8', '172.16.0.0/16']
l2 =['10.10.10.10','172.16.15.0/24','10.20.10.0/24','13.1.1.0/24','15.10.10.0/24','172.16.16.0/25','10.10.11.11']

[ip1 for ip1 in l1 for ip2 in l2 if IPNetwork(ip2) in IPNetwork(ip1)]


Output:



['10.0.0.0/8', '10.0.0.0/8', '10.0.0.0/8', '172.16.0.0/16', '172.16.0.0/16']


The above comprehension, in my opinion, isn't an optimal solution



I am looking to find a way by which I could re-implement the above solution with the added advantage of removing the elements (refer ip2) that are matched from l2 in every iteration, thus avoid rechecking those elements again in the next iteration










share|improve this question























  • Tip for the future: don't use the 'quote' markup to highlgiht text. Don't use capitals and refrain from using too many stylistic changes if the mere reason is to draw attention.
    – Bram Vanroy
    Nov 12 '18 at 11:00










  • Judging by your question and the auxiliary information, you might be trying to solve the wrong problem. Can you explain how supernet, networks and addresses translate to the two lists? Also, do you know how a set and item in container checks work?
    – MisterMiyagi
    Nov 12 '18 at 11:38










  • @MisterMiyagi I'm not sure what you mean by "translate to the two lists" but I have a clear problem; a list which has a CIRD/Supernet which is considered like a top of the tree. On the other side, I have another list with more-specific networks and addresses, and I want to categorize these addresses based upon the supernet
    – Mohamed Kamal
    Nov 12 '18 at 22:26











  • Does the number and order of matches matter?
    – MisterMiyagi
    Nov 14 '18 at 14:28













1












1








1







Problem:
There are 2 lists, one is considered the parent, and the other is the child. What I want to do is to construct a 3rd list based on a condition.



Current Solution:



from netaddr import *

l1 = ['10.0.0.0/8', '172.16.0.0/16']
l2 =['10.10.10.10','172.16.15.0/24','10.20.10.0/24','13.1.1.0/24','15.10.10.0/24','172.16.16.0/25','10.10.11.11']

[ip1 for ip1 in l1 for ip2 in l2 if IPNetwork(ip2) in IPNetwork(ip1)]


Output:



['10.0.0.0/8', '10.0.0.0/8', '10.0.0.0/8', '172.16.0.0/16', '172.16.0.0/16']


The above comprehension, in my opinion, isn't an optimal solution



I am looking to find a way by which I could re-implement the above solution with the added advantage of removing the elements (refer ip2) that are matched from l2 in every iteration, thus avoid rechecking those elements again in the next iteration










share|improve this question















Problem:
There are 2 lists, one is considered the parent, and the other is the child. What I want to do is to construct a 3rd list based on a condition.



Current Solution:



from netaddr import *

l1 = ['10.0.0.0/8', '172.16.0.0/16']
l2 =['10.10.10.10','172.16.15.0/24','10.20.10.0/24','13.1.1.0/24','15.10.10.0/24','172.16.16.0/25','10.10.11.11']

[ip1 for ip1 in l1 for ip2 in l2 if IPNetwork(ip2) in IPNetwork(ip1)]


Output:



['10.0.0.0/8', '10.0.0.0/8', '10.0.0.0/8', '172.16.0.0/16', '172.16.0.0/16']


The above comprehension, in my opinion, isn't an optimal solution



I am looking to find a way by which I could re-implement the above solution with the added advantage of removing the elements (refer ip2) that are matched from l2 in every iteration, thus avoid rechecking those elements again in the next iteration







python string python-3.x list counter






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 12:12

























asked Nov 11 '18 at 22:01









Mohamed Kamal

325




325











  • Tip for the future: don't use the 'quote' markup to highlgiht text. Don't use capitals and refrain from using too many stylistic changes if the mere reason is to draw attention.
    – Bram Vanroy
    Nov 12 '18 at 11:00










  • Judging by your question and the auxiliary information, you might be trying to solve the wrong problem. Can you explain how supernet, networks and addresses translate to the two lists? Also, do you know how a set and item in container checks work?
    – MisterMiyagi
    Nov 12 '18 at 11:38










  • @MisterMiyagi I'm not sure what you mean by "translate to the two lists" but I have a clear problem; a list which has a CIRD/Supernet which is considered like a top of the tree. On the other side, I have another list with more-specific networks and addresses, and I want to categorize these addresses based upon the supernet
    – Mohamed Kamal
    Nov 12 '18 at 22:26











  • Does the number and order of matches matter?
    – MisterMiyagi
    Nov 14 '18 at 14:28
















  • Tip for the future: don't use the 'quote' markup to highlgiht text. Don't use capitals and refrain from using too many stylistic changes if the mere reason is to draw attention.
    – Bram Vanroy
    Nov 12 '18 at 11:00










  • Judging by your question and the auxiliary information, you might be trying to solve the wrong problem. Can you explain how supernet, networks and addresses translate to the two lists? Also, do you know how a set and item in container checks work?
    – MisterMiyagi
    Nov 12 '18 at 11:38










  • @MisterMiyagi I'm not sure what you mean by "translate to the two lists" but I have a clear problem; a list which has a CIRD/Supernet which is considered like a top of the tree. On the other side, I have another list with more-specific networks and addresses, and I want to categorize these addresses based upon the supernet
    – Mohamed Kamal
    Nov 12 '18 at 22:26











  • Does the number and order of matches matter?
    – MisterMiyagi
    Nov 14 '18 at 14:28















Tip for the future: don't use the 'quote' markup to highlgiht text. Don't use capitals and refrain from using too many stylistic changes if the mere reason is to draw attention.
– Bram Vanroy
Nov 12 '18 at 11:00




Tip for the future: don't use the 'quote' markup to highlgiht text. Don't use capitals and refrain from using too many stylistic changes if the mere reason is to draw attention.
– Bram Vanroy
Nov 12 '18 at 11:00












Judging by your question and the auxiliary information, you might be trying to solve the wrong problem. Can you explain how supernet, networks and addresses translate to the two lists? Also, do you know how a set and item in container checks work?
– MisterMiyagi
Nov 12 '18 at 11:38




Judging by your question and the auxiliary information, you might be trying to solve the wrong problem. Can you explain how supernet, networks and addresses translate to the two lists? Also, do you know how a set and item in container checks work?
– MisterMiyagi
Nov 12 '18 at 11:38












@MisterMiyagi I'm not sure what you mean by "translate to the two lists" but I have a clear problem; a list which has a CIRD/Supernet which is considered like a top of the tree. On the other side, I have another list with more-specific networks and addresses, and I want to categorize these addresses based upon the supernet
– Mohamed Kamal
Nov 12 '18 at 22:26





@MisterMiyagi I'm not sure what you mean by "translate to the two lists" but I have a clear problem; a list which has a CIRD/Supernet which is considered like a top of the tree. On the other side, I have another list with more-specific networks and addresses, and I want to categorize these addresses based upon the supernet
– Mohamed Kamal
Nov 12 '18 at 22:26













Does the number and order of matches matter?
– MisterMiyagi
Nov 14 '18 at 14:28




Does the number and order of matches matter?
– MisterMiyagi
Nov 14 '18 at 14:28












1 Answer
1






active

oldest

votes


















0














Here a solution with counter and set.



Furthermore, it defines the IPNetwork objects only one time, which is the slowest operation (it costs 50µs when n2 in n1 costs only 5µs).



from collections import Counter
cnt=Counter()
S2=set(IPNetwork(ad2) for ad2 in l2)
for ad1 in l1:
n1=IPNetwork(ad1)
found=set()
for n2 in S2:
if n2 in n1:
cnt[ad1]+=1
found.add(n2)
S2 -= found


Finally cnt is Counter('10.0.0.0/8': 3, '172.16.0.0/16': 2) and S2 is IPNetwork('13.1.1.0/24'), IPNetwork('15.10.10.0/24').






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%2f53253691%2fdecrease-the-length-of-a-list-while-iterating-over-its-items%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














    Here a solution with counter and set.



    Furthermore, it defines the IPNetwork objects only one time, which is the slowest operation (it costs 50µs when n2 in n1 costs only 5µs).



    from collections import Counter
    cnt=Counter()
    S2=set(IPNetwork(ad2) for ad2 in l2)
    for ad1 in l1:
    n1=IPNetwork(ad1)
    found=set()
    for n2 in S2:
    if n2 in n1:
    cnt[ad1]+=1
    found.add(n2)
    S2 -= found


    Finally cnt is Counter('10.0.0.0/8': 3, '172.16.0.0/16': 2) and S2 is IPNetwork('13.1.1.0/24'), IPNetwork('15.10.10.0/24').






    share|improve this answer



























      0














      Here a solution with counter and set.



      Furthermore, it defines the IPNetwork objects only one time, which is the slowest operation (it costs 50µs when n2 in n1 costs only 5µs).



      from collections import Counter
      cnt=Counter()
      S2=set(IPNetwork(ad2) for ad2 in l2)
      for ad1 in l1:
      n1=IPNetwork(ad1)
      found=set()
      for n2 in S2:
      if n2 in n1:
      cnt[ad1]+=1
      found.add(n2)
      S2 -= found


      Finally cnt is Counter('10.0.0.0/8': 3, '172.16.0.0/16': 2) and S2 is IPNetwork('13.1.1.0/24'), IPNetwork('15.10.10.0/24').






      share|improve this answer

























        0












        0








        0






        Here a solution with counter and set.



        Furthermore, it defines the IPNetwork objects only one time, which is the slowest operation (it costs 50µs when n2 in n1 costs only 5µs).



        from collections import Counter
        cnt=Counter()
        S2=set(IPNetwork(ad2) for ad2 in l2)
        for ad1 in l1:
        n1=IPNetwork(ad1)
        found=set()
        for n2 in S2:
        if n2 in n1:
        cnt[ad1]+=1
        found.add(n2)
        S2 -= found


        Finally cnt is Counter('10.0.0.0/8': 3, '172.16.0.0/16': 2) and S2 is IPNetwork('13.1.1.0/24'), IPNetwork('15.10.10.0/24').






        share|improve this answer














        Here a solution with counter and set.



        Furthermore, it defines the IPNetwork objects only one time, which is the slowest operation (it costs 50µs when n2 in n1 costs only 5µs).



        from collections import Counter
        cnt=Counter()
        S2=set(IPNetwork(ad2) for ad2 in l2)
        for ad1 in l1:
        n1=IPNetwork(ad1)
        found=set()
        for n2 in S2:
        if n2 in n1:
        cnt[ad1]+=1
        found.add(n2)
        S2 -= found


        Finally cnt is Counter('10.0.0.0/8': 3, '172.16.0.0/16': 2) and S2 is IPNetwork('13.1.1.0/24'), IPNetwork('15.10.10.0/24').







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 14 '18 at 14:17

























        answered Nov 14 '18 at 13:13









        B. M.

        12.9k11934




        12.9k11934



























            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%2f53253691%2fdecrease-the-length-of-a-list-while-iterating-over-its-items%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

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo