Ensure that same name files will always match?











up vote
1
down vote

favorite












I have two list:
this:



list1



['C:\Users\user\Desktop\programs1\merge\AST\AST.shp',
'C:\Users\user\Desktop\programs1\merge\ASTI\ASTI.shp',
'C:\Users\user\Desktop\programs1\merge\ASTO\ASTO.shp']


and this:



list2



['C:\Users\user\Desktop\programs\merge\AST\AST.shp',
'C:\Users\user\Desktop\programs\merge\ASTI\ASTI.shp',
'C:\Users\user\Desktop\programs\merge\AWE\AWE.shp', #THIS IS EXTRA
'C:\Users\user\Desktop\programs\merge\ASTO\ASTO.shp']


Then zip:



final = [(f,s) for f,s in zip(list1,list2)]
final


How to ensure that the pairs will match with the corresponding same name on the other list after the zip?



Maybe we match with their previous folder?
Like:



if list1[0].split('\')[-2] == list2[0].split('\')[-2]:
final = [(f,s) for f,s in zip(list1,list2)]
final









share|improve this question









New contributor




user10671234 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • zip preserves the ordering. It's not clear to me what you are asking.
    – roganjosh
    Nov 19 at 14:03










  • in the second list the third item doesn't exist in the first. it will be paired wrong
    – user10671234
    Nov 19 at 14:06










  • And so will all subsequent items, then?
    – roganjosh
    Nov 19 at 14:07










  • Yes, this is why it needs to do some conditions to ensure that it pairs well.
    – user10671234
    Nov 19 at 14:09






  • 1




    why not [x for x in list1 if x in list2]? What do you need the zip for?
    – Ev. Kounis
    Nov 19 at 14:11















up vote
1
down vote

favorite












I have two list:
this:



list1



['C:\Users\user\Desktop\programs1\merge\AST\AST.shp',
'C:\Users\user\Desktop\programs1\merge\ASTI\ASTI.shp',
'C:\Users\user\Desktop\programs1\merge\ASTO\ASTO.shp']


and this:



list2



['C:\Users\user\Desktop\programs\merge\AST\AST.shp',
'C:\Users\user\Desktop\programs\merge\ASTI\ASTI.shp',
'C:\Users\user\Desktop\programs\merge\AWE\AWE.shp', #THIS IS EXTRA
'C:\Users\user\Desktop\programs\merge\ASTO\ASTO.shp']


Then zip:



final = [(f,s) for f,s in zip(list1,list2)]
final


How to ensure that the pairs will match with the corresponding same name on the other list after the zip?



Maybe we match with their previous folder?
Like:



if list1[0].split('\')[-2] == list2[0].split('\')[-2]:
final = [(f,s) for f,s in zip(list1,list2)]
final









share|improve this question









New contributor




user10671234 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • zip preserves the ordering. It's not clear to me what you are asking.
    – roganjosh
    Nov 19 at 14:03










  • in the second list the third item doesn't exist in the first. it will be paired wrong
    – user10671234
    Nov 19 at 14:06










  • And so will all subsequent items, then?
    – roganjosh
    Nov 19 at 14:07










  • Yes, this is why it needs to do some conditions to ensure that it pairs well.
    – user10671234
    Nov 19 at 14:09






  • 1




    why not [x for x in list1 if x in list2]? What do you need the zip for?
    – Ev. Kounis
    Nov 19 at 14:11













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have two list:
this:



list1



['C:\Users\user\Desktop\programs1\merge\AST\AST.shp',
'C:\Users\user\Desktop\programs1\merge\ASTI\ASTI.shp',
'C:\Users\user\Desktop\programs1\merge\ASTO\ASTO.shp']


and this:



list2



['C:\Users\user\Desktop\programs\merge\AST\AST.shp',
'C:\Users\user\Desktop\programs\merge\ASTI\ASTI.shp',
'C:\Users\user\Desktop\programs\merge\AWE\AWE.shp', #THIS IS EXTRA
'C:\Users\user\Desktop\programs\merge\ASTO\ASTO.shp']


Then zip:



final = [(f,s) for f,s in zip(list1,list2)]
final


How to ensure that the pairs will match with the corresponding same name on the other list after the zip?



Maybe we match with their previous folder?
Like:



if list1[0].split('\')[-2] == list2[0].split('\')[-2]:
final = [(f,s) for f,s in zip(list1,list2)]
final









share|improve this question









New contributor




user10671234 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I have two list:
this:



list1



['C:\Users\user\Desktop\programs1\merge\AST\AST.shp',
'C:\Users\user\Desktop\programs1\merge\ASTI\ASTI.shp',
'C:\Users\user\Desktop\programs1\merge\ASTO\ASTO.shp']


and this:



list2



['C:\Users\user\Desktop\programs\merge\AST\AST.shp',
'C:\Users\user\Desktop\programs\merge\ASTI\ASTI.shp',
'C:\Users\user\Desktop\programs\merge\AWE\AWE.shp', #THIS IS EXTRA
'C:\Users\user\Desktop\programs\merge\ASTO\ASTO.shp']


Then zip:



final = [(f,s) for f,s in zip(list1,list2)]
final


How to ensure that the pairs will match with the corresponding same name on the other list after the zip?



Maybe we match with their previous folder?
Like:



if list1[0].split('\')[-2] == list2[0].split('\')[-2]:
final = [(f,s) for f,s in zip(list1,list2)]
final






python list loops






share|improve this question









New contributor




user10671234 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




user10671234 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Nov 19 at 14:39





















New contributor




user10671234 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 19 at 14:02









user10671234

376




376




New contributor




user10671234 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





user10671234 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






user10671234 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • zip preserves the ordering. It's not clear to me what you are asking.
    – roganjosh
    Nov 19 at 14:03










  • in the second list the third item doesn't exist in the first. it will be paired wrong
    – user10671234
    Nov 19 at 14:06










  • And so will all subsequent items, then?
    – roganjosh
    Nov 19 at 14:07










  • Yes, this is why it needs to do some conditions to ensure that it pairs well.
    – user10671234
    Nov 19 at 14:09






  • 1




    why not [x for x in list1 if x in list2]? What do you need the zip for?
    – Ev. Kounis
    Nov 19 at 14:11


















  • zip preserves the ordering. It's not clear to me what you are asking.
    – roganjosh
    Nov 19 at 14:03










  • in the second list the third item doesn't exist in the first. it will be paired wrong
    – user10671234
    Nov 19 at 14:06










  • And so will all subsequent items, then?
    – roganjosh
    Nov 19 at 14:07










  • Yes, this is why it needs to do some conditions to ensure that it pairs well.
    – user10671234
    Nov 19 at 14:09






  • 1




    why not [x for x in list1 if x in list2]? What do you need the zip for?
    – Ev. Kounis
    Nov 19 at 14:11
















zip preserves the ordering. It's not clear to me what you are asking.
– roganjosh
Nov 19 at 14:03




zip preserves the ordering. It's not clear to me what you are asking.
– roganjosh
Nov 19 at 14:03












in the second list the third item doesn't exist in the first. it will be paired wrong
– user10671234
Nov 19 at 14:06




in the second list the third item doesn't exist in the first. it will be paired wrong
– user10671234
Nov 19 at 14:06












And so will all subsequent items, then?
– roganjosh
Nov 19 at 14:07




And so will all subsequent items, then?
– roganjosh
Nov 19 at 14:07












Yes, this is why it needs to do some conditions to ensure that it pairs well.
– user10671234
Nov 19 at 14:09




Yes, this is why it needs to do some conditions to ensure that it pairs well.
– user10671234
Nov 19 at 14:09




1




1




why not [x for x in list1 if x in list2]? What do you need the zip for?
– Ev. Kounis
Nov 19 at 14:11




why not [x for x in list1 if x in list2]? What do you need the zip for?
– Ev. Kounis
Nov 19 at 14:11












1 Answer
1






active

oldest

votes

















up vote
0
down vote













I hope I understand correctly that you want to get the pairs which match (for example based on the penultimate folder as in your example - you can customize on the condition you would like). What I would do then would be something like this:



dict1 = {path.split('\')[-2]: path for path in list1}
dict2 = {path.split('\')[-2]: path for path in list2}
common = set(dict1) & set(dict2)
pairs = [(dict1[element], dict2[element]) for element in common]





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',
    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
    });


    }
    });






    user10671234 is a new contributor. Be nice, and check out our Code of Conduct.










     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53376311%2fensure-that-same-name-files-will-always-match%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








    up vote
    0
    down vote













    I hope I understand correctly that you want to get the pairs which match (for example based on the penultimate folder as in your example - you can customize on the condition you would like). What I would do then would be something like this:



    dict1 = {path.split('\')[-2]: path for path in list1}
    dict2 = {path.split('\')[-2]: path for path in list2}
    common = set(dict1) & set(dict2)
    pairs = [(dict1[element], dict2[element]) for element in common]





    share|improve this answer

























      up vote
      0
      down vote













      I hope I understand correctly that you want to get the pairs which match (for example based on the penultimate folder as in your example - you can customize on the condition you would like). What I would do then would be something like this:



      dict1 = {path.split('\')[-2]: path for path in list1}
      dict2 = {path.split('\')[-2]: path for path in list2}
      common = set(dict1) & set(dict2)
      pairs = [(dict1[element], dict2[element]) for element in common]





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        I hope I understand correctly that you want to get the pairs which match (for example based on the penultimate folder as in your example - you can customize on the condition you would like). What I would do then would be something like this:



        dict1 = {path.split('\')[-2]: path for path in list1}
        dict2 = {path.split('\')[-2]: path for path in list2}
        common = set(dict1) & set(dict2)
        pairs = [(dict1[element], dict2[element]) for element in common]





        share|improve this answer












        I hope I understand correctly that you want to get the pairs which match (for example based on the penultimate folder as in your example - you can customize on the condition you would like). What I would do then would be something like this:



        dict1 = {path.split('\')[-2]: path for path in list1}
        dict2 = {path.split('\')[-2]: path for path in list2}
        common = set(dict1) & set(dict2)
        pairs = [(dict1[element], dict2[element]) for element in common]






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 at 22:55









        zsomko

        3616




        3616






















            user10671234 is a new contributor. Be nice, and check out our Code of Conduct.










             

            draft saved


            draft discarded


















            user10671234 is a new contributor. Be nice, and check out our Code of Conduct.













            user10671234 is a new contributor. Be nice, and check out our Code of Conduct.












            user10671234 is a new contributor. Be nice, and check out our Code of Conduct.















             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53376311%2fensure-that-same-name-files-will-always-match%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