Replace items in list with items in tuple of tuples












0















currently I am trying to unify license names in rpm packages.



Therefore I parse rpms, get the license information as lists and want to compare these list with a lookup table I build.



Here is an example:



lic = ['GPLv2', ' & ', 'LGPLv2+', ' & ', 'LGPLv2.1+', ' | ', 'LGPLv3+']

duplicates = (
('GPL-2.0', 'GPL-2', 'GPLv2', 'GPLv2.0'),
('GPL-2.0+', 'GPL-2+', 'GPLv2+', 'GPLv2.0+'),
('GPL-3.0', 'GPL-3', 'GPLv3', 'GPLv3.0'),
('GPL-3.0+', 'GPL-3+', 'GPLv3+', 'GPLv3.0+'),
('LGPL-2.0', 'LGPLv2.0', 'LGPLv2', 'LGPL2'),
('LGPL-2.0+', 'LGPLv2.0+', 'LGPLv2+', 'LGPL2+'),
('LGPL-2.1', 'LGPLv2.1', 'LGPL2.1'),
('LGPL-2.1+', 'LGPLv2.1+', 'LGPL2.1+'),
('LGPL-3.0', 'LGPLv3.0', 'LGPLv3', 'LGPL3'),
('LGPL-3.0+', 'LGPLv3.0+', 'LGPLv3+', 'LGPL3+')
)


What would be the most efficient and most pythonic way of replacing all occurrences of any element in duplicates in lic with the corresponding first element of each tuple?



eg: GPLv2 needs to be replaced with GPL-2.0, LGPLv2+ with LGPL-2.0+ and so on.



I am on Python 3.5.2










share|improve this question























  • You probably need to use a dictionary.

    – Rakesh
    Nov 27 '18 at 13:38
















0















currently I am trying to unify license names in rpm packages.



Therefore I parse rpms, get the license information as lists and want to compare these list with a lookup table I build.



Here is an example:



lic = ['GPLv2', ' & ', 'LGPLv2+', ' & ', 'LGPLv2.1+', ' | ', 'LGPLv3+']

duplicates = (
('GPL-2.0', 'GPL-2', 'GPLv2', 'GPLv2.0'),
('GPL-2.0+', 'GPL-2+', 'GPLv2+', 'GPLv2.0+'),
('GPL-3.0', 'GPL-3', 'GPLv3', 'GPLv3.0'),
('GPL-3.0+', 'GPL-3+', 'GPLv3+', 'GPLv3.0+'),
('LGPL-2.0', 'LGPLv2.0', 'LGPLv2', 'LGPL2'),
('LGPL-2.0+', 'LGPLv2.0+', 'LGPLv2+', 'LGPL2+'),
('LGPL-2.1', 'LGPLv2.1', 'LGPL2.1'),
('LGPL-2.1+', 'LGPLv2.1+', 'LGPL2.1+'),
('LGPL-3.0', 'LGPLv3.0', 'LGPLv3', 'LGPL3'),
('LGPL-3.0+', 'LGPLv3.0+', 'LGPLv3+', 'LGPL3+')
)


What would be the most efficient and most pythonic way of replacing all occurrences of any element in duplicates in lic with the corresponding first element of each tuple?



eg: GPLv2 needs to be replaced with GPL-2.0, LGPLv2+ with LGPL-2.0+ and so on.



I am on Python 3.5.2










share|improve this question























  • You probably need to use a dictionary.

    – Rakesh
    Nov 27 '18 at 13:38














0












0








0








currently I am trying to unify license names in rpm packages.



Therefore I parse rpms, get the license information as lists and want to compare these list with a lookup table I build.



Here is an example:



lic = ['GPLv2', ' & ', 'LGPLv2+', ' & ', 'LGPLv2.1+', ' | ', 'LGPLv3+']

duplicates = (
('GPL-2.0', 'GPL-2', 'GPLv2', 'GPLv2.0'),
('GPL-2.0+', 'GPL-2+', 'GPLv2+', 'GPLv2.0+'),
('GPL-3.0', 'GPL-3', 'GPLv3', 'GPLv3.0'),
('GPL-3.0+', 'GPL-3+', 'GPLv3+', 'GPLv3.0+'),
('LGPL-2.0', 'LGPLv2.0', 'LGPLv2', 'LGPL2'),
('LGPL-2.0+', 'LGPLv2.0+', 'LGPLv2+', 'LGPL2+'),
('LGPL-2.1', 'LGPLv2.1', 'LGPL2.1'),
('LGPL-2.1+', 'LGPLv2.1+', 'LGPL2.1+'),
('LGPL-3.0', 'LGPLv3.0', 'LGPLv3', 'LGPL3'),
('LGPL-3.0+', 'LGPLv3.0+', 'LGPLv3+', 'LGPL3+')
)


What would be the most efficient and most pythonic way of replacing all occurrences of any element in duplicates in lic with the corresponding first element of each tuple?



eg: GPLv2 needs to be replaced with GPL-2.0, LGPLv2+ with LGPL-2.0+ and so on.



I am on Python 3.5.2










share|improve this question














currently I am trying to unify license names in rpm packages.



Therefore I parse rpms, get the license information as lists and want to compare these list with a lookup table I build.



Here is an example:



lic = ['GPLv2', ' & ', 'LGPLv2+', ' & ', 'LGPLv2.1+', ' | ', 'LGPLv3+']

duplicates = (
('GPL-2.0', 'GPL-2', 'GPLv2', 'GPLv2.0'),
('GPL-2.0+', 'GPL-2+', 'GPLv2+', 'GPLv2.0+'),
('GPL-3.0', 'GPL-3', 'GPLv3', 'GPLv3.0'),
('GPL-3.0+', 'GPL-3+', 'GPLv3+', 'GPLv3.0+'),
('LGPL-2.0', 'LGPLv2.0', 'LGPLv2', 'LGPL2'),
('LGPL-2.0+', 'LGPLv2.0+', 'LGPLv2+', 'LGPL2+'),
('LGPL-2.1', 'LGPLv2.1', 'LGPL2.1'),
('LGPL-2.1+', 'LGPLv2.1+', 'LGPL2.1+'),
('LGPL-3.0', 'LGPLv3.0', 'LGPLv3', 'LGPL3'),
('LGPL-3.0+', 'LGPLv3.0+', 'LGPLv3+', 'LGPL3+')
)


What would be the most efficient and most pythonic way of replacing all occurrences of any element in duplicates in lic with the corresponding first element of each tuple?



eg: GPLv2 needs to be replaced with GPL-2.0, LGPLv2+ with LGPL-2.0+ and so on.



I am on Python 3.5.2







python list tuples






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 27 '18 at 13:34









normannorman

31




31













  • You probably need to use a dictionary.

    – Rakesh
    Nov 27 '18 at 13:38



















  • You probably need to use a dictionary.

    – Rakesh
    Nov 27 '18 at 13:38

















You probably need to use a dictionary.

– Rakesh
Nov 27 '18 at 13:38





You probably need to use a dictionary.

– Rakesh
Nov 27 '18 at 13:38












2 Answers
2






active

oldest

votes


















0














You could iterate over your lic and compare each of its elements with duplicates and if you find match replace that element with first element of matching tuple.



lic = ['GPLv2', ' & ', 'LGPLv2+', ' & ', 'LGPLv2.1+', ' | ', 'LGPLv3+']

duplicates = (
('GPL-2.0', 'GPL-2', 'GPLv2', 'GPLv2.0'),
('GPL-2.0+', 'GPL-2+', 'GPLv2+', 'GPLv2.0+'),
('GPL-3.0', 'GPL-3', 'GPLv3', 'GPLv3.0'),
('GPL-3.0+', 'GPL-3+', 'GPLv3+', 'GPLv3.0+'),
('LGPL-2.0', 'LGPLv2.0', 'LGPLv2', 'LGPL2'),
('LGPL-2.0+', 'LGPLv2.0+', 'LGPLv2+', 'LGPL2+'),
('LGPL-2.1', 'LGPLv2.1', 'LGPL2.1'),
('LGPL-2.1+', 'LGPLv2.1+', 'LGPL2.1+'),
('LGPL-3.0', 'LGPLv3.0', 'LGPLv3', 'LGPL3'),
('LGPL-3.0+', 'LGPLv3.0+', 'LGPLv3+', 'LGPL3+')
)

for idx, i in enumerate(lic):
for match in duplicates:
if i in match:
lic[idx] = match[0]
break

print(lic)


Output:



['GPL-2.0', ' & ', 'LGPL-2.0+', ' & ', 'LGPL-2.1+', ' | ', 'LGPL-3.0+']





share|improve this answer































    1














    I would modify the duplicates tuple of tuples to a dict first. like so:



    duplicates = {k: v for k, *v in duplicates}  # {'GPL-2.0': ['GPL-2', 'GPLv2', 'GPLv2.0'], ...}


    and then do:



    lic_clean = [next((k for k, v in duplicates.items() if x in v), x) for x in lic]


    which produces:



    ['GPL-2.0', ' & ', 'LGPL-2.0+', ' & ', 'LGPL-2.1+', ' | ', 'LGPL-3.0+']





    share|improve this answer


























    • Thank you. I want to keep the seperators, so I can reassemble the original license afterwards

      – norman
      Nov 27 '18 at 13:47











    • @norman See the edit then

      – Ev. Kounis
      Nov 27 '18 at 13:48











    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%2f53500942%2freplace-items-in-list-with-items-in-tuple-of-tuples%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You could iterate over your lic and compare each of its elements with duplicates and if you find match replace that element with first element of matching tuple.



    lic = ['GPLv2', ' & ', 'LGPLv2+', ' & ', 'LGPLv2.1+', ' | ', 'LGPLv3+']

    duplicates = (
    ('GPL-2.0', 'GPL-2', 'GPLv2', 'GPLv2.0'),
    ('GPL-2.0+', 'GPL-2+', 'GPLv2+', 'GPLv2.0+'),
    ('GPL-3.0', 'GPL-3', 'GPLv3', 'GPLv3.0'),
    ('GPL-3.0+', 'GPL-3+', 'GPLv3+', 'GPLv3.0+'),
    ('LGPL-2.0', 'LGPLv2.0', 'LGPLv2', 'LGPL2'),
    ('LGPL-2.0+', 'LGPLv2.0+', 'LGPLv2+', 'LGPL2+'),
    ('LGPL-2.1', 'LGPLv2.1', 'LGPL2.1'),
    ('LGPL-2.1+', 'LGPLv2.1+', 'LGPL2.1+'),
    ('LGPL-3.0', 'LGPLv3.0', 'LGPLv3', 'LGPL3'),
    ('LGPL-3.0+', 'LGPLv3.0+', 'LGPLv3+', 'LGPL3+')
    )

    for idx, i in enumerate(lic):
    for match in duplicates:
    if i in match:
    lic[idx] = match[0]
    break

    print(lic)


    Output:



    ['GPL-2.0', ' & ', 'LGPL-2.0+', ' & ', 'LGPL-2.1+', ' | ', 'LGPL-3.0+']





    share|improve this answer




























      0














      You could iterate over your lic and compare each of its elements with duplicates and if you find match replace that element with first element of matching tuple.



      lic = ['GPLv2', ' & ', 'LGPLv2+', ' & ', 'LGPLv2.1+', ' | ', 'LGPLv3+']

      duplicates = (
      ('GPL-2.0', 'GPL-2', 'GPLv2', 'GPLv2.0'),
      ('GPL-2.0+', 'GPL-2+', 'GPLv2+', 'GPLv2.0+'),
      ('GPL-3.0', 'GPL-3', 'GPLv3', 'GPLv3.0'),
      ('GPL-3.0+', 'GPL-3+', 'GPLv3+', 'GPLv3.0+'),
      ('LGPL-2.0', 'LGPLv2.0', 'LGPLv2', 'LGPL2'),
      ('LGPL-2.0+', 'LGPLv2.0+', 'LGPLv2+', 'LGPL2+'),
      ('LGPL-2.1', 'LGPLv2.1', 'LGPL2.1'),
      ('LGPL-2.1+', 'LGPLv2.1+', 'LGPL2.1+'),
      ('LGPL-3.0', 'LGPLv3.0', 'LGPLv3', 'LGPL3'),
      ('LGPL-3.0+', 'LGPLv3.0+', 'LGPLv3+', 'LGPL3+')
      )

      for idx, i in enumerate(lic):
      for match in duplicates:
      if i in match:
      lic[idx] = match[0]
      break

      print(lic)


      Output:



      ['GPL-2.0', ' & ', 'LGPL-2.0+', ' & ', 'LGPL-2.1+', ' | ', 'LGPL-3.0+']





      share|improve this answer


























        0












        0








        0







        You could iterate over your lic and compare each of its elements with duplicates and if you find match replace that element with first element of matching tuple.



        lic = ['GPLv2', ' & ', 'LGPLv2+', ' & ', 'LGPLv2.1+', ' | ', 'LGPLv3+']

        duplicates = (
        ('GPL-2.0', 'GPL-2', 'GPLv2', 'GPLv2.0'),
        ('GPL-2.0+', 'GPL-2+', 'GPLv2+', 'GPLv2.0+'),
        ('GPL-3.0', 'GPL-3', 'GPLv3', 'GPLv3.0'),
        ('GPL-3.0+', 'GPL-3+', 'GPLv3+', 'GPLv3.0+'),
        ('LGPL-2.0', 'LGPLv2.0', 'LGPLv2', 'LGPL2'),
        ('LGPL-2.0+', 'LGPLv2.0+', 'LGPLv2+', 'LGPL2+'),
        ('LGPL-2.1', 'LGPLv2.1', 'LGPL2.1'),
        ('LGPL-2.1+', 'LGPLv2.1+', 'LGPL2.1+'),
        ('LGPL-3.0', 'LGPLv3.0', 'LGPLv3', 'LGPL3'),
        ('LGPL-3.0+', 'LGPLv3.0+', 'LGPLv3+', 'LGPL3+')
        )

        for idx, i in enumerate(lic):
        for match in duplicates:
        if i in match:
        lic[idx] = match[0]
        break

        print(lic)


        Output:



        ['GPL-2.0', ' & ', 'LGPL-2.0+', ' & ', 'LGPL-2.1+', ' | ', 'LGPL-3.0+']





        share|improve this answer













        You could iterate over your lic and compare each of its elements with duplicates and if you find match replace that element with first element of matching tuple.



        lic = ['GPLv2', ' & ', 'LGPLv2+', ' & ', 'LGPLv2.1+', ' | ', 'LGPLv3+']

        duplicates = (
        ('GPL-2.0', 'GPL-2', 'GPLv2', 'GPLv2.0'),
        ('GPL-2.0+', 'GPL-2+', 'GPLv2+', 'GPLv2.0+'),
        ('GPL-3.0', 'GPL-3', 'GPLv3', 'GPLv3.0'),
        ('GPL-3.0+', 'GPL-3+', 'GPLv3+', 'GPLv3.0+'),
        ('LGPL-2.0', 'LGPLv2.0', 'LGPLv2', 'LGPL2'),
        ('LGPL-2.0+', 'LGPLv2.0+', 'LGPLv2+', 'LGPL2+'),
        ('LGPL-2.1', 'LGPLv2.1', 'LGPL2.1'),
        ('LGPL-2.1+', 'LGPLv2.1+', 'LGPL2.1+'),
        ('LGPL-3.0', 'LGPLv3.0', 'LGPLv3', 'LGPL3'),
        ('LGPL-3.0+', 'LGPLv3.0+', 'LGPLv3+', 'LGPL3+')
        )

        for idx, i in enumerate(lic):
        for match in duplicates:
        if i in match:
        lic[idx] = match[0]
        break

        print(lic)


        Output:



        ['GPL-2.0', ' & ', 'LGPL-2.0+', ' & ', 'LGPL-2.1+', ' | ', 'LGPL-3.0+']






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 27 '18 at 13:47









        Filip MłynarskiFilip Młynarski

        1,7961413




        1,7961413

























            1














            I would modify the duplicates tuple of tuples to a dict first. like so:



            duplicates = {k: v for k, *v in duplicates}  # {'GPL-2.0': ['GPL-2', 'GPLv2', 'GPLv2.0'], ...}


            and then do:



            lic_clean = [next((k for k, v in duplicates.items() if x in v), x) for x in lic]


            which produces:



            ['GPL-2.0', ' & ', 'LGPL-2.0+', ' & ', 'LGPL-2.1+', ' | ', 'LGPL-3.0+']





            share|improve this answer


























            • Thank you. I want to keep the seperators, so I can reassemble the original license afterwards

              – norman
              Nov 27 '18 at 13:47











            • @norman See the edit then

              – Ev. Kounis
              Nov 27 '18 at 13:48
















            1














            I would modify the duplicates tuple of tuples to a dict first. like so:



            duplicates = {k: v for k, *v in duplicates}  # {'GPL-2.0': ['GPL-2', 'GPLv2', 'GPLv2.0'], ...}


            and then do:



            lic_clean = [next((k for k, v in duplicates.items() if x in v), x) for x in lic]


            which produces:



            ['GPL-2.0', ' & ', 'LGPL-2.0+', ' & ', 'LGPL-2.1+', ' | ', 'LGPL-3.0+']





            share|improve this answer


























            • Thank you. I want to keep the seperators, so I can reassemble the original license afterwards

              – norman
              Nov 27 '18 at 13:47











            • @norman See the edit then

              – Ev. Kounis
              Nov 27 '18 at 13:48














            1












            1








            1







            I would modify the duplicates tuple of tuples to a dict first. like so:



            duplicates = {k: v for k, *v in duplicates}  # {'GPL-2.0': ['GPL-2', 'GPLv2', 'GPLv2.0'], ...}


            and then do:



            lic_clean = [next((k for k, v in duplicates.items() if x in v), x) for x in lic]


            which produces:



            ['GPL-2.0', ' & ', 'LGPL-2.0+', ' & ', 'LGPL-2.1+', ' | ', 'LGPL-3.0+']





            share|improve this answer















            I would modify the duplicates tuple of tuples to a dict first. like so:



            duplicates = {k: v for k, *v in duplicates}  # {'GPL-2.0': ['GPL-2', 'GPLv2', 'GPLv2.0'], ...}


            and then do:



            lic_clean = [next((k for k, v in duplicates.items() if x in v), x) for x in lic]


            which produces:



            ['GPL-2.0', ' & ', 'LGPL-2.0+', ' & ', 'LGPL-2.1+', ' | ', 'LGPL-3.0+']






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 27 '18 at 13:48

























            answered Nov 27 '18 at 13:42









            Ev. KounisEv. Kounis

            11k21548




            11k21548













            • Thank you. I want to keep the seperators, so I can reassemble the original license afterwards

              – norman
              Nov 27 '18 at 13:47











            • @norman See the edit then

              – Ev. Kounis
              Nov 27 '18 at 13:48



















            • Thank you. I want to keep the seperators, so I can reassemble the original license afterwards

              – norman
              Nov 27 '18 at 13:47











            • @norman See the edit then

              – Ev. Kounis
              Nov 27 '18 at 13:48

















            Thank you. I want to keep the seperators, so I can reassemble the original license afterwards

            – norman
            Nov 27 '18 at 13:47





            Thank you. I want to keep the seperators, so I can reassemble the original license afterwards

            – norman
            Nov 27 '18 at 13:47













            @norman See the edit then

            – Ev. Kounis
            Nov 27 '18 at 13:48





            @norman See the edit then

            – Ev. Kounis
            Nov 27 '18 at 13:48


















            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53500942%2freplace-items-in-list-with-items-in-tuple-of-tuples%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

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

            Calculate evaluation metrics using cross_val_predict sklearn

            Insert data from modal to MySQL (multiple modal on website)